Skip to content

Fix CircularByteBuffer.peek() matching bytes past the buffered data - #876

Open
lenamonj wants to merge 1 commit into
apache:masterfrom
lenamonj:io-peek-past-buffered
Open

lenamonj wants to merge 1 commit into
apache:masterfrom
lenamonj:io-peek-past-buffered

Conversation

@lenamonj

@lenamonj lenamonj commented Sep 7, 2026

Copy link
Copy Markdown

CircularByteBuffer.peek() returned false early only when length was smaller than the number of buffered bytes. A larger length fell through to the comparison loop, which read past the valid bytes into whatever the backing array held there, so a fresh buffer holding {1, 2} reported an exact match for {1, 2, 0, 0}. Through PeekableInputStream.peek() a two-byte stream matched a four-byte pattern, and a scanner branching on that result took the branch for content the stream does not contain.

The guard now returns false unless length equals the buffered count, the exact-match rule IO-855 documented.

Tests: testPeekLongerThanBuffered and testPeekLongerThanInputDoesNotMatch, red on master and green with the change. Default Maven goal green.

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute? Claude (Anthropic) found the defect and drafted the fix and tests; I reviewed them, reproduced the failure on a fresh clone of master, and ran the default Maven goal.
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

peek() returned false early only when length was smaller than the number
of buffered bytes. A larger length fell through to the comparison loop,
which read past the valid bytes into the backing array, so a buffer
holding two bytes reported an exact match for a four-byte pattern.
Return false unless length equals the buffered count.
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.

1 participant