Skip to content

Fix integer overflow in CodedInputStream::ReadLengthAndPushLimit - #29523

Open
TurboRx wants to merge 1 commit into
protocolbuffers:mainfrom
TurboRx:fix-coded-input-stream-readlength-overflow
Open

Fix integer overflow in CodedInputStream::ReadLengthAndPushLimit#29523
TurboRx wants to merge 1 commit into
protocolbuffers:mainfrom
TurboRx:fix-coded-input-stream-readlength-overflow

Conversation

@TurboRx

@TurboRx TurboRx commented Aug 30, 2026

Copy link
Copy Markdown

When ReadVarint32 decodes a length > INT_MAX, casting uint32_t to signed int makes byte_limit negative. This causes PushLimit to skip updating current_limit_, leaving the stream bounded only by outer limits.

This patch clamps lengths > INT_MAX to 0 so the stream is immediately bounded, and adds a unit test for this case in coded_stream_unittest.cc.

When ReadVarint32 decodes a length greater than INT_MAX (such as 0x80000000),
implicitly passing it as a signed int to PushLimit results in a negative
byte_limit. Because PushLimit checks byte_limit >= 0, the new limit is
silently ignored, leaving the stream bounded only by outer/unconstrained
limits instead of immediately bounding the stream.

This change ensures that lengths exceeding INT_MAX safely trigger a limit of 0,
immediately bounding the stream.
@runzw

runzw commented Aug 31, 2026

Copy link
Copy Markdown
Member

The direction of this PR is right, but PushLimit already rejects negative limits, so the pre-existing behavior was already "length ignored / bounded by outer limit," not an exploitable "unconstrained negative limit."

Before merging, could you:

  1. make the PR description more accurate
  2. add more boundary tests (INT_MAX, UINT_MAX, and a normal case), now only 0x8000000 is tested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants