Skip to content

Commit

Permalink
Add tests that CodedOutputStream.spaceLeft isn't negative after overf…
Browse files Browse the repository at this point in the history
…low.

Not all encoders pass this test yet.

PiperOrigin-RevId: 676726356
  • Loading branch information
mhansen authored and copybara-github committed Sep 20, 2024
1 parent 6bf86f0 commit a7c758f
Show file tree
Hide file tree
Showing 2 changed files with 277 additions and 218 deletions.
16 changes: 14 additions & 2 deletions java/core/src/main/java/com/google/protobuf/CodedOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -1306,12 +1306,14 @@ final void writeMessageNoTag(final MessageLite value, Schema schema) throws IOEx

@Override
public final void write(byte value) throws IOException {
int position = this.position;
try {
buffer[position++] = value;
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e);
}
this.position = position; // Only update position if we stayed within the array bounds.
}

@Override
Expand Down Expand Up @@ -2042,7 +2044,12 @@ public void writeUInt32NoTag(int value) throws IOException {

@Override
public void writeFixed32NoTag(int value) throws IOException {
buffer.putInt(bufferPos(position), value);
try {
buffer.putInt(bufferPos(position), value);
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 4), e);
}
position += FIXED32_SIZE;
}

Expand Down Expand Up @@ -2076,7 +2083,12 @@ public void writeUInt64NoTag(long value) throws IOException {

@Override
public void writeFixed64NoTag(long value) throws IOException {
buffer.putLong(bufferPos(position), value);
try {
buffer.putLong(bufferPos(position), value);
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 4), e);
}
position += FIXED64_SIZE;
}

Expand Down
Loading

0 comments on commit a7c758f

Please sign in to comment.