diff options
author | jasonh@google.com <jasonh@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2011-02-18 04:35:54 +0000 |
---|---|---|
committer | jasonh@google.com <jasonh@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2011-02-18 04:35:54 +0000 |
commit | eab9b5d47faf6c9611be323f4354f9548f6e378a (patch) | |
tree | e0ef8ff542c417000b76e7ded7327338dc65562a /java/src | |
parent | 17d57dbbcd01b90c536f7ee39415a864e26869d2 (diff) | |
download | protobuf-eab9b5d47faf6c9611be323f4354f9548f6e378a.tar.gz protobuf-eab9b5d47faf6c9611be323f4354f9548f6e378a.tar.bz2 protobuf-eab9b5d47faf6c9611be323f4354f9548f6e378a.zip |
Fix issue 256: compute the correct size for negative enum values, which need to be sign extended.
Diffstat (limited to 'java/src')
-rw-r--r-- | java/src/main/java/com/google/protobuf/CodedOutputStream.java | 2 | ||||
-rw-r--r-- | java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java | 15 |
2 files changed, 16 insertions, 1 deletions
diff --git a/java/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/src/main/java/com/google/protobuf/CodedOutputStream.java index f3582e2d..ac5f2d30 100644 --- a/java/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -751,7 +751,7 @@ public final class CodedOutputStream { * Caller is responsible for converting the enum value to its numeric value. */ public static int computeEnumSizeNoTag(final int value) { - return computeRawVarint32Size(value); + return computeInt32SizeNoTag(value); } /** diff --git a/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java b/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java index 48e54657..74476e36 100644 --- a/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java +++ b/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java @@ -300,4 +300,19 @@ public class CodedOutputStreamTest extends TestCase { assertEqualBytes(TestUtil.getGoldenPackedFieldsMessage().toByteArray(), rawBytes); } + + /** Test writing a message containing a negative enum value. This used to + * fail because the size was not properly computed as a sign-extended varint. */ + public void testWriteMessageWithNegativeEnumValue() throws Exception { + protobuf_unittest.UnittestProto.SparseEnumMessage message = + protobuf_unittest.UnittestProto.SparseEnumMessage.newBuilder() + .setSparseEnum(protobuf_unittest.UnittestProto.TestSparseEnum.SPARSE_E) + .build(); + assertTrue(message.getSparseEnum().getNumber() < 0); + byte[] rawBytes = message.toByteArray(); + protobuf_unittest.UnittestProto.SparseEnumMessage message2 = + protobuf_unittest.UnittestProto.SparseEnumMessage.parseFrom(rawBytes); + assertEquals(protobuf_unittest.UnittestProto.TestSparseEnum.SPARSE_E, + message2.getSparseEnum()); + } } |