Skip to content

Conversation

@niloc132
Copy link

This gives the InputStream slightly less work to do when it knows there are no buffers that can be checked for more data.

Also loosened a check in CodedInputStream to permit read() to return zero, as this is documented as a valid value - though in this case -1 is the correct value as there is nothing more to read.

Fixes #23957

@niloc132 niloc132 requested a review from a team as a code owner October 15, 2025 19:59
@niloc132 niloc132 requested review from zhangskz and removed request for a team October 15, 2025 19:59
@niloc132 niloc132 changed the title Fix JavaIterableByteBufferInputStream to return -1 on empty Fix Java IterableByteBufferInputStream to return -1 on empty Oct 15, 2025
This gives the InputStream slightly less work to do when it knows there
are no buffers that can be checked for more data.

Also loosened a check in CodedInputStream to permit read() to return
zero, as this is documented as a valid value - though in this case -1 is
the correct value as there is nothing more to read.

Fixes protocolbuffers#23957
if (!getNextByteBuffer()) {
currentByteBuffer = EMPTY_BYTE_BUFFER;
currentIndex = 0;
currentIndex = dataSize;
Copy link
Member

Choose a reason for hiding this comment

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

Why was this changed? I don't think this seems necessary.

Copy link
Author

Choose a reason for hiding this comment

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

It isn't the only correct way to fix it, but something like this should probably be done. While returning 0 from read() is not wrong, it is also not correct for this case - the return value 0 means "zero bytes were read this time", but the return value -1 means "this is the end, there is no more data to read, do not try again".

If we just go with the relaxed check in CodedInputStream, it would technically be correct to keep trying to ask for more data, because 0 only means that this read found nothing, but not yet reached end of stream.

It could also be correct to instead assign dataSize to be 0, so that it matches currentIndex. The bug is that in both overloads of read(), the two must be equal, or we don't return -1 right away (which would be correct, since there is no more data to read and there never will be). But since dataSize is never assigned outside the constructor, it seems more appropriate to advance currentIndex to meet dataSize, rather than change dataSize to match whatever currentSize we happen to be at (in the case of the bug, one past the end of dataSIze, so we fail the checks at the top of the read()s).

@zhangskz zhangskz added the 🅰️ safe for tests Mark a commit as safe to run presubmits over label Oct 15, 2025
@github-actions github-actions bot removed the 🅰️ safe for tests Mark a commit as safe to run presubmits over label Oct 15, 2025
@zhangskz zhangskz added java wait for user action 🅰️ safe for tests Mark a commit as safe to run presubmits over labels Oct 15, 2025
@github-actions github-actions bot added untriaged auto added to all issues by default when created. and removed 🅰️ safe for tests Mark a commit as safe to run presubmits over wait for user action labels Oct 16, 2025
@zhangskz zhangskz added wait for user action 🅰️ safe for tests Mark a commit as safe to run presubmits over and removed untriaged auto added to all issues by default when created. wait for user action 🅰️ safe for tests Mark a commit as safe to run presubmits over labels Oct 16, 2025
@github-actions github-actions bot removed the 🅰️ safe for tests Mark a commit as safe to run presubmits over label Oct 16, 2025
copybara-service bot pushed a commit that referenced this pull request Oct 16, 2025
…user action` to `untriaged` when unstale.

Looks like this is really meant to exempt that label from the action. Try this out in case this also includes exempting the *removal* of that label from counting as a unstaleness event. This shouldn't hurt since github bot removes the tag after tests run anyways.

Example: #23994
Documentation: https://github.com/actions/stale
PiperOrigin-RevId: 820294398
copybara-service bot pushed a commit that referenced this pull request Oct 16, 2025
…user action` to `untriaged` when unstale.

Looks like this is really meant to exempt that label from the action. Try this out in case this also includes exempting the *removal* of that label from counting as a unstaleness event. This shouldn't hurt since github bot removes the tag after tests run anyways.

Example: #23994
Documentation: https://github.com/actions/stale
PiperOrigin-RevId: 820319199
@github-actions github-actions bot added untriaged auto added to all issues by default when created. and removed wait for user action labels Oct 17, 2025
@zhangskz zhangskz added wait for user action 🅰️ safe for tests Mark a commit as safe to run presubmits over and removed untriaged auto added to all issues by default when created. labels Oct 17, 2025
@github-actions github-actions bot added untriaged auto added to all issues by default when created. and removed 🅰️ safe for tests Mark a commit as safe to run presubmits over wait for user action labels Oct 17, 2025
@honglooker honglooker removed the untriaged auto added to all issues by default when created. label Oct 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: CodedInputStream.newInstance(List) with an empty bytebuffer produces an error

3 participants