Skip to content

fix off-by-one boundaries in QUIC bytesNeededForInteger - #3628

Merged
Lukasa merged 2 commits into
apple:mainfrom
jmestwa-coder:quic-varint-off-by-one
Jun 22, 2026
Merged

Lukasa merged 2 commits into
apple:mainfrom
jmestwa-coder:quic-varint-off-by-one

Conversation

@jmestwa-coder

Copy link
Copy Markdown
Contributor

Off-by-one in the QUIC variable-length integer length boundaries.

Motivation:

bytesNeededForInteger classifies an integer into the 1/2/4/8 byte QUIC encoding using half-open ranges (0..<63, 0..<16383, 0..<1_073_741_823, 0..<4_611_686_018_427_387_903). Those upper bounds are the largest value each size can hold (2^6-1, 2^14-1, 2^30-1 and 2^62-1 per RFC 9000 Β§ 16), so each maximum is wrongly excluded:

  • 63, 16383 and 2^30-1 report one byte too many, so the encoder writes them with a larger encoding than needed.
  • 2^62-1, the largest value the encoding can represent, falls through to the fatalError and crashes the encoder.

The existing one-byte write test only iterates 0..<63, so the 63 case was never exercised.

Modifications:

  • Switch the four ranges in bytesNeededForInteger to inclusive bounds.
  • Extend the one-byte write test to include 63, and add boundary checks plus a 2^62-1 round-trip test.

Result:

Boundary values encode in the minimal number of bytes and 2^62-1 round-trips instead of crashing.

@simonjbeaumont simonjbeaumont left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

@Lukasa Lukasa added the πŸ”¨ semver/patch No public API change. label Jun 22, 2026
@Lukasa
Lukasa enabled auto-merge (squash) June 22, 2026 12:31
@Lukasa
Lukasa merged commit 0b38af6 into apple:main Jun 22, 2026
49 of 55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

πŸ”¨ semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants