Skip to content

Commit 0e75d92

Browse files
mhansencopybara-github
authored andcommitted
Convert IndexOutOfBoundsException to OutOfSpaceException in UnsafeDirectNioEncoder
When writing fixed32 and fixed64. PiperOrigin-RevId: 677938044
1 parent 39824ca commit 0e75d92

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

java/core/src/main/java/com/google/protobuf/CodedOutputStream.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,12 @@ public void writeUInt32NoTag(int value) throws IOException {
20442044

20452045
@Override
20462046
public void writeFixed32NoTag(int value) throws IOException {
2047-
buffer.putInt(bufferPos(position), value);
2047+
try {
2048+
buffer.putInt(bufferPos(position), value);
2049+
} catch (IndexOutOfBoundsException e) {
2050+
throw new OutOfSpaceException(
2051+
String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED32_SIZE), e);
2052+
}
20482053
position += FIXED32_SIZE;
20492054
}
20502055

@@ -2078,7 +2083,12 @@ public void writeUInt64NoTag(long value) throws IOException {
20782083

20792084
@Override
20802085
public void writeFixed64NoTag(long value) throws IOException {
2081-
buffer.putLong(bufferPos(position), value);
2086+
try {
2087+
buffer.putLong(bufferPos(position), value);
2088+
} catch (IndexOutOfBoundsException e) {
2089+
throw new OutOfSpaceException(
2090+
String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED64_SIZE), e);
2091+
}
20822092
position += FIXED64_SIZE;
20832093
}
20842094

java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,9 @@ public void testWriteFixed32NoTag_outOfBounds_throws() throws Exception {
291291
// Streaming's buffering masks out of bounds writes.
292292
assume().that(outputType).isNotEqualTo(OutputType.STREAM);
293293

294-
Class<? extends Exception> e = OutOfSpaceException.class;
295-
// UnsafeDirectNioEncoder is incorrectly throwing IndexOutOfBoundsException for some operations.
296-
if (outputType == OutputType.NIO_DIRECT_UNSAFE
297-
|| outputType == OutputType.NIO_DIRECT_UNSAFE_WITH_INITIAL_OFFSET) {
298-
e = IndexOutOfBoundsException.class;
299-
}
300-
301294
for (int i = 0; i < 4; i++) {
302295
Coder coder = outputType.newCoder(i);
303-
assertThrows(e, () -> coder.stream().writeFixed32NoTag(1));
296+
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed32NoTag(1));
304297
}
305298
}
306299

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

312-
Class<? extends Exception> e = OutOfSpaceException.class;
313-
// UnsafeDirectNioEncoder is incorrectly throwing IndexOutOfBoundsException for some operations.
314-
if (outputType == OutputType.NIO_DIRECT_UNSAFE
315-
|| outputType == OutputType.NIO_DIRECT_UNSAFE_WITH_INITIAL_OFFSET) {
316-
e = IndexOutOfBoundsException.class;
317-
}
318-
319305
for (int i = 0; i < 8; i++) {
320306
Coder coder = outputType.newCoder(i);
321-
assertThrows(e, () -> coder.stream().writeFixed64NoTag(1));
307+
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed64NoTag(1));
322308
}
323309
}
324310

0 commit comments

Comments
 (0)