summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Duff <bduff@google.com>2015-04-21 23:33:41 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2015-04-21 23:33:41 +0000
commitc68dba6080b13aba4f72bcf616f29af8e0bf617e (patch)
treec17053aa432de03c0d2213c63670fe3c4f3f6aa7
parent2eadf946678a8a8d3cd56188454ab106b8dc5a39 (diff)
parenta07081b82c9a7b3da4a4a0cb577748c66bf2bf9e (diff)
downloadexternal_protobuf-c68dba6080b13aba4f72bcf616f29af8e0bf617e.zip
external_protobuf-c68dba6080b13aba4f72bcf616f29af8e0bf617e.tar.gz
external_protobuf-c68dba6080b13aba4f72bcf616f29af8e0bf617e.tar.bz2
am a07081b8: am 63b42c8d: am 92f1bf25: Merge "Throw OutOfSpaceException instead of IllegalArgumentException."
* commit 'a07081b82c9a7b3da4a4a0cb577748c66bf2bf9e': Throw OutOfSpaceException instead of IllegalArgumentException.
-rw-r--r--java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java11
-rw-r--r--java/src/test/java/com/google/protobuf/NanoTest.java18
2 files changed, 28 insertions, 1 deletions
diff --git a/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java b/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
index 324a63f..53060da 100644
--- a/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
+++ b/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
@@ -300,6 +300,12 @@ public final class CodedOutputByteBufferNano {
final int maxLengthVarIntSize = computeRawVarint32Size(value.length() * MAX_UTF8_EXPANSION);
if (minLengthVarIntSize == maxLengthVarIntSize) {
int oldPosition = buffer.position();
+ // Buffer.position, when passed a position that is past its limit, throws
+ // IllegalArgumentException, and this class is documented to throw
+ // OutOfSpaceException instead.
+ if (buffer.remaining() < minLengthVarIntSize) {
+ throw new OutOfSpaceException(oldPosition + minLengthVarIntSize, buffer.limit());
+ }
buffer.position(oldPosition + minLengthVarIntSize);
encode(value, buffer);
int newPosition = buffer.position();
@@ -311,7 +317,10 @@ public final class CodedOutputByteBufferNano {
encode(value, buffer);
}
} catch (BufferOverflowException e) {
- throw new OutOfSpaceException(buffer.position(), buffer.limit());
+ final OutOfSpaceException outOfSpaceException = new OutOfSpaceException(buffer.position(),
+ buffer.limit());
+ outOfSpaceException.initCause(e);
+ throw outOfSpaceException;
}
}
diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java
index 4c3b416..ef1572a 100644
--- a/java/src/test/java/com/google/protobuf/NanoTest.java
+++ b/java/src/test/java/com/google/protobuf/NanoTest.java
@@ -31,6 +31,7 @@
package com.google.protobuf;
import com.google.protobuf.nano.CodedInputByteBufferNano;
+import com.google.protobuf.nano.CodedOutputByteBufferNano;
import com.google.protobuf.nano.EnumClassNanoMultiple;
import com.google.protobuf.nano.EnumClassNanos;
import com.google.protobuf.nano.EnumValidity;
@@ -2340,6 +2341,23 @@ public class NanoTest extends TestCase {
}
}
+ /** Regression test for https://github.com/google/protobuf/issues/292 */
+ public void testCorrectExceptionThrowWhenEncodingStringsWithoutEnoughSpace() throws Exception {
+ String testCase = "Foooooooo";
+ assertEquals(CodedOutputByteBufferNano.computeRawVarint32Size(testCase.length()),
+ CodedOutputByteBufferNano.computeRawVarint32Size(testCase.length() * 3));
+ assertEquals(11, CodedOutputByteBufferNano.computeStringSize(1, testCase));
+ // Tag is one byte, varint describing string length is 1 byte, string length is 9 bytes.
+ // An array of size 1 will cause a failure when trying to write the varint.
+ for (int i = 0; i < 11; i++) {
+ CodedOutputByteBufferNano bufferNano = CodedOutputByteBufferNano.newInstance(new byte[i]);
+ try {
+ bufferNano.writeString(1, testCase);
+ fail("Should have thrown an out of space exception");
+ } catch (CodedOutputByteBufferNano.OutOfSpaceException expected) {}
+ }
+ }
+
private void testEncodingOfString(char c, int length) throws InvalidProtocolBufferNanoException {
TestAllTypesNano testAllTypesNano = new TestAllTypesNano();
final String fullString = fullString(c, length);