Skip to content

Fix division by zero in CubicCongestionControlGetNetworkStatistics#5837

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-division-by-zero-error
Draft

Fix division by zero in CubicCongestionControlGetNetworkStatistics#5837
Copilot wants to merge 3 commits intomainfrom
copilot/fix-division-by-zero-error

Conversation

Copy link
Contributor

Copilot AI commented Mar 3, 2026

SmoothedRtt is zero-initialized and only set after the first RTT sample, so calling GetNetworkStatistics early in connection lifetime (e.g., QUIC_PARAM_CONN_STATISTICS_V2) triggers STATUS_INTEGER_DIVIDE_BY_ZERO on Windows.

Description

  • src/core/cubic.c: Guard the bandwidth calculation against zero SmoothedRtt:
    NetworkStatistics->Bandwidth =
        Path->SmoothedRtt == 0 ? 0 : Cubic->CongestionWindow / Path->SmoothedRtt;

Testing

New unit test CubicTest.GetNetworkStatistics_ZeroRtt in src/core/unittest/CubicTest.cpp calls GetNetworkStatistics before any RTT sample is received (SmoothedRtt == 0) and asserts Bandwidth == 0.

Documentation

No documentation impact.

Original prompt

This section details on the original issue you should resolve

<issue_title>[DeepTest] Division by zero in CubicCongestionControlGetNetworkStatistics</issue_title>
<issue_description># Location: src/core/cubic.c, line 433
NetworkStatistics->Bandwidth = Cubic->CongestionWindow / Path->SmoothedRtt;

Root cause: GetNetworkStatistics divides CongestionWindow by SmoothedRtt without checking whether SmoothedRtt is zero. SmoothedRtt is initialized to 0 (via CxPlatZeroMemory in connection init) and only set to a non-zero value after the first RTT sample is received.

Impact: Any call to GetNetworkStatistics before the first RTT sample (e.g., via MsQuicGetParam with QUIC_PARAM_CONN_STATISTICS_V2 early in connection lifetime) causes a STATUS_INTEGER_DIVIDE_BY_ZERO structured exception, crashing the process.

Affected OS

  • Windows
  • Linux
  • macOS
  • Other (specify below)

Additional OS information

No response

MsQuic version

main

Steps taken to reproduce bug

  1. Add the following test case to src/core/unittest/CubicTest.cpp
TEST(CubicTest, DeepTest_Bug_GetNetworkStatsDivByZero)
{
    QUIC_CONNECTION Connection;
    QUIC_SETTINGS_INTERNAL Settings{};
    Settings.InitialWindowPackets = 10;
    Settings.SendIdleTimeoutMs = 1000;

    InitializeMockConnection(Connection, 1280);
    // SmoothedRtt is 0 by default (no RTT sample received yet)
    // GotFirstRttSample is FALSE by default

    CubicCongestionControlInitialize(&Connection.CongestionControl, &Settings);

    QUIC_NETWORK_STATISTICS NetworkStats;
    CxPlatZeroMemory(&NetworkStats, sizeof(NetworkStats));

    // This will crash with STATUS_INTEGER_DIVIDE_BY_ZERO
    // because SmoothedRtt=0 and line 433 does: CongestionWindow / SmoothedRtt
    Connection.CongestionControl.QuicCongestionControlGetNetworkStatistics(
        &Connection,
        &Connection.CongestionControl,
        &NetworkStats);

    // If the bug were fixed, we'd expect:
    ASSERT_EQ(NetworkStats.Bandwidth, 0u); // Graceful when no RTT sample
}
  1. Build and run the test case with filter CubicTest.DeepTest_Bug_GetNetworkStatsDivByZero

Expected behavior

Should return Bandwidth=0 when SmoothedRtt=0 (graceful degradation)

Actual outcome

STATUS_INTEGER_DIVIDE_BY_ZERO crash

Additional details

No response</issue_description>

<agent_instructions>Fix and add test</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits March 3, 2026 01:50
…d add test

Co-authored-by: saikat107 <2145576+saikat107@users.noreply.github.com>
Co-authored-by: saikat107 <2145576+saikat107@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix division by zero in GetNetworkStatistics function Fix division by zero in CubicCongestionControlGetNetworkStatistics Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DeepTest] Division by zero in CubicCongestionControlGetNetworkStatistics

2 participants