protocolbuffers/protobuf

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

Aperta

#23.957 aperta il 14 ott 2025

 (3 commenti) (0 reazioni) (0 assegnatari)C++ (16.128 fork)batch import
help wantedjava

Metriche repository

Star
 (71.223 stelle)
Metriche merge PR
 (Merge medio 2g 11h) (185 PR mergiate in 30 g)

Descrizione

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:

  1. Create a CodedInputStream with an empty list
  2. Confirm that readTag() returns 0
  3. Create a CodedInputStream with a single empty ByteBuffer
  4. 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());

Guida contributor