protocolbuffers/protobuf

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

Open

#23,957 opened on Oct 14, 2025

View on GitHub
 (3 comments) (0 reactions) (0 assignees)C++ (16,128 forks)batch import
help wantedjava

Repository metrics

Stars
 (71,223 stars)
PR merge metrics
 (Avg merge 2d 11h) (185 merged PRs in 30d)

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:

  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());

Contributor guide