diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java index df38bd02c6bb3..cffa4cfb2e687 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -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; } @@ -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; } diff --git a/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java b/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java index c4f20360e2f5d..9e6529696a555 100644 --- a/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java +++ b/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java @@ -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 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)); } } @@ -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 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)); } }