Skip to content

Commit bc24489

Browse files
mhansencopybara-github
authored andcommitted
Throw more informative OutOfSpaceExceptions when we run out of space serializing a proto.
Before, some encoders would not give any details about position/limit/length. Now a few more places do. Just found this while trying to add some tests for the exception message, and found some encoders weren't setting it. This doesn't fix all the places that OutOfSpaceException didn't have a useful message. PiperOrigin-RevId: 681218740
1 parent 02d6885 commit bc24489

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ public void write(byte value) throws IOException {
16661666
try {
16671667
buffer.put(value);
16681668
} catch (BufferOverflowException e) {
1669-
throw new OutOfSpaceException(e);
1669+
throw new OutOfSpaceException(buffer.position(), buffer.limit(), 1, e);
16701670
}
16711671
}
16721672

@@ -1725,7 +1725,7 @@ public void writeFixed32NoTag(int value) throws IOException {
17251725
try {
17261726
buffer.putInt(value);
17271727
} catch (BufferOverflowException e) {
1728-
throw new OutOfSpaceException(e);
1728+
throw new OutOfSpaceException(buffer.position(), buffer.limit(), FIXED32_SIZE, e);
17291729
}
17301730
}
17311731

@@ -1751,7 +1751,7 @@ public void writeFixed64NoTag(long value) throws IOException {
17511751
try {
17521752
buffer.putLong(value);
17531753
} catch (BufferOverflowException e) {
1754-
throw new OutOfSpaceException(e);
1754+
throw new OutOfSpaceException(buffer.position(), buffer.limit(), FIXED64_SIZE, e);
17551755
}
17561756
}
17571757

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package com.google.protobuf;
99

1010
import static com.google.common.truth.Truth.assertThat;
11-
import static com.google.common.truth.Truth.assertWithMessage;
1211
import static com.google.common.truth.TruthJUnit.assume;
1312
import static org.junit.Assert.assertThrows;
1413

@@ -293,7 +292,9 @@ public void testWriteFixed32NoTag_outOfBounds_throws() throws Exception {
293292

294293
for (int i = 0; i < 4; i++) {
295294
Coder coder = outputType.newCoder(i);
296-
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed32NoTag(1));
295+
OutOfSpaceException e =
296+
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed32NoTag(1));
297+
assertThat(e).hasMessageThat().contains("len: 4");
297298
assertThat(coder.stream().spaceLeft()).isEqualTo(i);
298299
}
299300
}
@@ -305,7 +306,9 @@ public void testWriteFixed64NoTag_outOfBounds_throws() throws Exception {
305306

306307
for (int i = 0; i < 8; i++) {
307308
Coder coder = outputType.newCoder(i);
308-
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed64NoTag(1));
309+
OutOfSpaceException e =
310+
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed64NoTag(1));
311+
assertThat(e).hasMessageThat().contains("len: 8");
309312
assertThat(coder.stream().spaceLeft()).isEqualTo(i);
310313
}
311314
}
@@ -577,7 +580,9 @@ public void testWriteByte() throws Exception {
577580
if (outputType == OutputType.STREAM) {
578581
return;
579582
}
580-
assertThrows(OutOfSpaceException.class, () -> coder.stream().write((byte) 1));
583+
OutOfSpaceException e =
584+
assertThrows(OutOfSpaceException.class, () -> coder.stream().write((byte) 1));
585+
assertThat(e).hasMessageThat().contains("len: 1");
581586
if (outputType.supportsSpaceLeft()) {
582587
assertThat(coder.stream().spaceLeft()).isEqualTo(0);
583588
}
@@ -673,12 +678,10 @@ public void testSerializeInvalidUtf8FollowedByOutOfSpace() throws Exception {
673678
Coder coder = outputType.newCoder(notEnoughBytes);
674679

675680
String invalidString = newString(Character.MIN_HIGH_SURROGATE, 'f', 'o', 'o', 'b', 'a', 'r');
676-
try {
677-
coder.stream().writeStringNoTag(invalidString);
678-
assertWithMessage("Expected OutOfSpaceException").fail();
679-
} catch (OutOfSpaceException e) {
680-
assertThat(e).hasCauseThat().isInstanceOf(IndexOutOfBoundsException.class);
681-
}
681+
OutOfSpaceException e =
682+
assertThrows(
683+
OutOfSpaceException.class, () -> coder.stream().writeStringNoTag(invalidString));
684+
assertThat(e).hasCauseThat().isInstanceOf(IndexOutOfBoundsException.class);
682685
}
683686

684687
/** Regression test for https://github.com/protocolbuffers/protobuf/issues/292 */

0 commit comments

Comments
 (0)