Skip to content

Commit

Permalink
Convert IndexOutOfBoundsException to OutOfSpaceException in UnsafeDir…
Browse files Browse the repository at this point in the history
…ectNioEncoder

When writing fixed32 and fixed64.

PiperOrigin-RevId: 677938044
  • Loading branch information
mhansen authored and copybara-github committed Sep 23, 2024
1 parent 39824ca commit 0e75d92
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
14 changes: 12 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 @@ -2044,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, FIXED32_SIZE), e);
}
position += FIXED32_SIZE;
}

Expand Down Expand Up @@ -2078,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, FIXED64_SIZE), e);
}
position += FIXED64_SIZE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,16 +291,9 @@ public void testWriteFixed32NoTag_outOfBounds_throws() throws Exception {
// Streaming's buffering masks out of bounds writes.
assume().that(outputType).isNotEqualTo(OutputType.STREAM);

Class<? extends Exception> e = OutOfSpaceException.class;
// UnsafeDirectNioEncoder is incorrectly throwing IndexOutOfBoundsException for some operations.
if (outputType == OutputType.NIO_DIRECT_UNSAFE
|| outputType == OutputType.NIO_DIRECT_UNSAFE_WITH_INITIAL_OFFSET) {
e = IndexOutOfBoundsException.class;
}

for (int i = 0; i < 4; i++) {
Coder coder = outputType.newCoder(i);
assertThrows(e, () -> coder.stream().writeFixed32NoTag(1));
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed32NoTag(1));
}
}

Expand All @@ -309,16 +302,9 @@ public void testWriteFixed64NoTag_outOfBounds_throws() throws Exception {
// Streaming's buffering masks out of bounds writes.
assume().that(outputType).isNotEqualTo(OutputType.STREAM);

Class<? extends Exception> e = OutOfSpaceException.class;
// UnsafeDirectNioEncoder is incorrectly throwing IndexOutOfBoundsException for some operations.
if (outputType == OutputType.NIO_DIRECT_UNSAFE
|| outputType == OutputType.NIO_DIRECT_UNSAFE_WITH_INITIAL_OFFSET) {
e = IndexOutOfBoundsException.class;
}

for (int i = 0; i < 8; i++) {
Coder coder = outputType.newCoder(i);
assertThrows(e, () -> coder.stream().writeFixed64NoTag(1));
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed64NoTag(1));
}
}

Expand Down

0 comments on commit 0e75d92

Please sign in to comment.