Skip to content

Commit

Permalink
CodedOutputStream: avoid updating position to go beyond end of array.
Browse files Browse the repository at this point in the history
In writeFixed32NoTag and writeFixed64NoTag

This is a partial roll-forward of cl/673588324.

PiperOrigin-RevId: 678435934
  • Loading branch information
mhansen authored and copybara-github committed Sep 24, 2024
1 parent b5d3320 commit 76ab5f2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,7 @@ public final void writeUInt32NoTag(int value) throws IOException {

@Override
public final void writeFixed32NoTag(int value) throws IOException {
int position = this.position; // Perf: hoist field to register to avoid load/stores.
try {
buffer[position++] = (byte) (value & 0xFF);
buffer[position++] = (byte) ((value >> 8) & 0xFF);
Expand All @@ -1355,6 +1356,7 @@ public final void writeFixed32NoTag(int value) throws IOException {
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 @@ -1389,6 +1391,7 @@ public final void writeUInt64NoTag(long value) throws IOException {

@Override
public final void writeFixed64NoTag(long value) throws IOException {
int position = this.position; // Perf: hoist field to register to avoid load/stores.
try {
buffer[position++] = (byte) ((int) (value) & 0xFF);
buffer[position++] = (byte) ((int) (value >> 8) & 0xFF);
Expand All @@ -1402,6 +1405,7 @@ public final void writeFixed64NoTag(long value) throws IOException {
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
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,7 @@ public void testWriteFixed32NoTag_outOfBounds_throws() throws Exception {
for (int i = 0; i < 4; i++) {
Coder coder = outputType.newCoder(i);
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed32NoTag(1));
// These OutputTypes are not behaving correctly yet.
if (outputType != OutputType.ARRAY
&& outputType != OutputType.NIO_HEAP
&& outputType != OutputType.NIO_HEAP_WITH_INITIAL_OFFSET) {
assertThat(coder.stream().spaceLeft()).isEqualTo(i);
}
assertThat(coder.stream().spaceLeft()).isEqualTo(i);
}
}

Expand All @@ -311,12 +306,7 @@ public void testWriteFixed64NoTag_outOfBounds_throws() throws Exception {
for (int i = 0; i < 8; i++) {
Coder coder = outputType.newCoder(i);
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed64NoTag(1));
// These OutputTypes are not behaving correctly yet.
if (outputType != OutputType.ARRAY
&& outputType != OutputType.NIO_HEAP
&& outputType != OutputType.NIO_HEAP_WITH_INITIAL_OFFSET) {
assertThat(coder.stream().spaceLeft()).isEqualTo(i);
}
assertThat(coder.stream().spaceLeft()).isEqualTo(i);
}
}

Expand Down

0 comments on commit 76ab5f2

Please sign in to comment.