Skip to content

Commit

Permalink
Throw more informative OutOfSpaceExceptions when we run out of space …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
mhansen authored and copybara-github committed Oct 2, 2024
1 parent 02d6885 commit bc24489
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@ public void write(byte value) throws IOException {
try {
buffer.put(value);
} catch (BufferOverflowException e) {
throw new OutOfSpaceException(e);
throw new OutOfSpaceException(buffer.position(), buffer.limit(), 1, e);
}
}

Expand Down Expand Up @@ -1725,7 +1725,7 @@ public void writeFixed32NoTag(int value) throws IOException {
try {
buffer.putInt(value);
} catch (BufferOverflowException e) {
throw new OutOfSpaceException(e);
throw new OutOfSpaceException(buffer.position(), buffer.limit(), FIXED32_SIZE, e);
}
}

Expand All @@ -1751,7 +1751,7 @@ public void writeFixed64NoTag(long value) throws IOException {
try {
buffer.putLong(value);
} catch (BufferOverflowException e) {
throw new OutOfSpaceException(e);
throw new OutOfSpaceException(buffer.position(), buffer.limit(), FIXED64_SIZE, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package com.google.protobuf;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.common.truth.TruthJUnit.assume;
import static org.junit.Assert.assertThrows;

Expand Down Expand Up @@ -293,7 +292,9 @@ 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));
OutOfSpaceException e =
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed32NoTag(1));
assertThat(e).hasMessageThat().contains("len: 4");
assertThat(coder.stream().spaceLeft()).isEqualTo(i);
}
}
Expand All @@ -305,7 +306,9 @@ 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));
OutOfSpaceException e =
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed64NoTag(1));
assertThat(e).hasMessageThat().contains("len: 8");
assertThat(coder.stream().spaceLeft()).isEqualTo(i);
}
}
Expand Down Expand Up @@ -577,7 +580,9 @@ public void testWriteByte() throws Exception {
if (outputType == OutputType.STREAM) {
return;
}
assertThrows(OutOfSpaceException.class, () -> coder.stream().write((byte) 1));
OutOfSpaceException e =
assertThrows(OutOfSpaceException.class, () -> coder.stream().write((byte) 1));
assertThat(e).hasMessageThat().contains("len: 1");
if (outputType.supportsSpaceLeft()) {
assertThat(coder.stream().spaceLeft()).isEqualTo(0);
}
Expand Down Expand Up @@ -673,12 +678,10 @@ public void testSerializeInvalidUtf8FollowedByOutOfSpace() throws Exception {
Coder coder = outputType.newCoder(notEnoughBytes);

String invalidString = newString(Character.MIN_HIGH_SURROGATE, 'f', 'o', 'o', 'b', 'a', 'r');
try {
coder.stream().writeStringNoTag(invalidString);
assertWithMessage("Expected OutOfSpaceException").fail();
} catch (OutOfSpaceException e) {
assertThat(e).hasCauseThat().isInstanceOf(IndexOutOfBoundsException.class);
}
OutOfSpaceException e =
assertThrows(
OutOfSpaceException.class, () -> coder.stream().writeStringNoTag(invalidString));
assertThat(e).hasCauseThat().isInstanceOf(IndexOutOfBoundsException.class);
}

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

0 comments on commit bc24489

Please sign in to comment.