- 
                Notifications
    You must be signed in to change notification settings 
- Fork 15.9k
Open
Labels
Description
What version of protobuf and what language are you using?
Language: Java: originally found in 3.25.4, still present in 4.33.0-RC2
What operating system (Linux, Windows, ...) and version?
Tested MacOS 15.6.1, Linux 5.14.0
What runtime / compiler are you using (e.g., python version or gcc version)
N/A
What did you do?
Steps to reproduce the behavior:
- Create a CodedInputStream with an empty list
- Confirm that readTag() returns 0
- Create a CodedInputStream with a single empty ByteBuffer
- Call readTag()
import com.google.protobuf.CodedInputStream
import java.nio.ByteBuffer
import java.util.List
public class App {
  public static void main(String[] args) {
    CodedInputStream cis = CodedInputStream.newInstance(List.of())
    System.out.println(cis.readTag());//success, returns 0
    cis = CodedInputStream.newInstance(List.of(ByteBuffer.wrap(new byte[0])));
    System.out.println(cis.readTag());//failure, throws exception
  }
}What did you expect to see
Should return zero, same as readTag() for an empty list
What did you see instead?
Exception java.lang.IllegalStateException: class com.google.protobuf.IterableByteBufferInputStream#read(byte[]) returned invalid result: 0
The InputStream implementation is buggy.
      at CodedInputStream$StreamDecoder.tryRefillBuffer (CodedInputStream.java:2882)
      at CodedInputStream$StreamDecoder.isAtEnd (CodedInputStream.java:2794)
      at CodedInputStream$StreamDecoder.readTag (CodedInputStream.java:2151)
Note that empty ByteBuffers within a larger collection do not seem to cause a problem with some quick testing, just a collection with only empty ByteBuffers in it.
CodedInputStream cis = CodedInputStream.newInstance(List.of(ByteBuffer.allocate(0), ByteBuffer.wrap(new byte[]{0x0a}), ByteBuffer.wrap(new byte[]{0x01, 0x61})))
System.out.println(cis.readTag());// success
System.out.println(cis.readString());