Skip to content

Reject negative size in mergeDelimitedFrom#28097

Closed
TristanInSec wants to merge 1 commit into
protocolbuffers:mainfrom
TristanInSec:fix/merge-delimited-negative-size
Closed

Reject negative size in mergeDelimitedFrom#28097
TristanInSec wants to merge 1 commit into
protocolbuffers:mainfrom
TristanInSec:fix/merge-delimited-negative-size

Conversation

@TristanInSec

Copy link
Copy Markdown
Contributor

Summary

mergeDelimitedFrom passes the return value of readRawVarint32 directly to LimitedInputStream without checking for negative values. For 5-byte varints encoding values >= 2^31, readRawVarint32 returns a negative int (e.g., \x80\x80\x80\x80\x08 decodes to -2147483648).

LimitedInputStream with a negative limit immediately returns EOF (if (limit <= 0) return -1), causing the parser to produce a default/empty message and return true (success). The actual message body bytes remain unconsumed in the stream, permanently desynchronizing subsequent parseDelimitedFrom calls.

Fix

Add if (size < 0) throw InvalidProtocolBufferException.negativeSize() before constructing LimitedInputStream.

Test plan

  • Verified with PoC: 5-byte varint prefix encoding 2^31 previously produced empty message + stream desync; after fix, negativeSize exception thrown
  • Existing unit tests pass

readRawVarint32 returns a negative int for 5-byte varints encoding
values >= 2^31. mergeDelimitedFrom passes this directly to
LimitedInputStream, which treats negative limits as EOF. This silently
produces an empty message and leaves the actual body bytes unconsumed,
desynchronizing subsequent parseDelimitedFrom calls on the same stream.

Add a negative size check before constructing LimitedInputStream.
@TristanInSec
TristanInSec requested a review from a team as a code owner June 19, 2026 21:29
@TristanInSec
TristanInSec requested review from googleberg and removed request for a team June 19, 2026 21:29

@esrauchg esrauchg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@esrauchg esrauchg added the 🅰️ safe for tests Mark a commit as safe to run presubmits over label Jun 22, 2026
@github-actions github-actions Bot removed the 🅰️ safe for tests Mark a commit as safe to run presubmits over label Jun 22, 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.

2 participants