Skip to content

Commit 1a53cd5

Browse files
committed
Fix #85
1 parent 78bd410 commit 1a53cd5

File tree

4 files changed

+75
-10
lines changed

4 files changed

+75
-10
lines changed

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,8 +2382,8 @@ protected final int _decode32Bits() throws IOException {
23822382
return _slow32();
23832383
}
23842384
final byte[] b = _inputBuffer;
2385-
int v = (b[ptr] & 0xFF) + ((b[ptr+1]) << 8)
2386-
+ ((b[ptr+2] & 0xFF) << 16) + (b[ptr+3] << 24);
2385+
int v = (b[ptr] & 0xFF) + ((b[ptr+1] & 0xFF) << 8)
2386+
+ ((b[ptr+2] & 0xFF) << 16) + ((b[ptr+3] & 0xFF) << 24);
23872387
_inputPtr = ptr+4;
23882388
return v;
23892389
}

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufTestBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ protected void assertToken(JsonToken expToken, JsonToken actToken)
485485
}
486486
}
487487

488-
protected void assertToken(JsonToken expToken, JsonParser jp)
488+
protected void assertToken(JsonToken expToken, JsonParser p)
489489
{
490-
assertToken(expToken, jp.getCurrentToken());
490+
assertToken(expToken, p.getCurrentToken());
491491
}
492492

493493
protected void assertType(Object ob, Class<?> expType)

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/WritePrimitiveArrayTest.java

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ public class WritePrimitiveArrayTest extends ProtobufTestBase
4848
+" repeated double values = 1 [packed=true];\n"
4949
+"}\n"
5050
;
51+
52+
final protected static String PROTOC_FLOAT_ARRAY_SPARSE = "message Floats {\n"
53+
+" repeated float values = 1;\n"
54+
+"}\n"
55+
;
56+
57+
final protected static String PROTOC_FLOAT_ARRAY_PACKED = "message Floats {\n"
58+
+" repeated float values = 1 [packed=true];\n"
59+
+"}\n"
60+
;
5161

5262
static class IntArray {
5363
public int[] values;
@@ -75,8 +85,17 @@ public DoubleArray(double... v) {
7585
values = v;
7686
}
7787
}
78-
79-
final ObjectMapper MAPPER = new ObjectMapper(new ProtobufFactory());
88+
89+
static class FloatArray {
90+
public float[] values;
91+
92+
protected FloatArray() { }
93+
public FloatArray(float... v) {
94+
values = v;
95+
}
96+
}
97+
98+
final ObjectMapper MAPPER = new ProtobufMapper();
8099

81100
public WritePrimitiveArrayTest() throws Exception { }
82101

@@ -205,9 +224,9 @@ public void testDoubleArraySparse() throws Exception
205224
{
206225
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_DOUBLE_ARRAY_SPARSE);
207226
final ObjectWriter w = MAPPER.writer(schema);
208-
DoubleArray input = new DoubleArray(0.25, -2.5, 1000.125);
227+
DoubleArray input = new DoubleArray(0.25, -2.5, 1000.125, 1234567891234567890.5);
209228
byte[] bytes = w.writeValueAsBytes(input);
210-
assertEquals(27, bytes.length);
229+
assertEquals(36, bytes.length);
211230

212231
DoubleArray result = MAPPER.readerFor(DoubleArray.class).with(schema)
213232
.readValue(bytes);
@@ -218,9 +237,9 @@ public void testDoubleArrayPacked() throws Exception
218237
{
219238
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_DOUBLE_ARRAY_PACKED);
220239
final ObjectWriter w = MAPPER.writer(schema);
221-
DoubleArray input = new DoubleArray(-0.5, 89245.25, 0.625);
240+
DoubleArray input = new DoubleArray(-0.5, 89245.25, 0.625, 1234567891234567890.5);
222241
byte[] bytes = w.writeValueAsBytes(input);
223-
assertEquals(26, bytes.length);
242+
assertEquals(34, bytes.length);
224243

225244
DoubleArray result = MAPPER.readerFor(DoubleArray.class).with(schema)
226245
.readValue(bytes);
@@ -237,4 +256,48 @@ private void _assertEquals(double[] exp, double[] act)
237256
}
238257
}
239258
}
259+
260+
/*
261+
/**********************************************************
262+
/* Test methods, floating-point arrays
263+
/**********************************************************
264+
*/
265+
266+
public void testfloatArraySparse() throws Exception
267+
{
268+
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_FLOAT_ARRAY_SPARSE);
269+
final ObjectWriter w = MAPPER.writer(schema);
270+
FloatArray input = new FloatArray(0.25f, -2.5f, 55555555.5f);
271+
byte[] bytes = w.writeValueAsBytes(input);
272+
assertEquals(15, bytes.length);
273+
274+
FloatArray result = MAPPER.readerFor(FloatArray.class).with(schema)
275+
.readValue(bytes);
276+
_assertEquals(input.values, result.values);
277+
}
278+
279+
public void testfloatArrayPacked() throws Exception
280+
{
281+
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_FLOAT_ARRAY_PACKED);
282+
final ObjectWriter w = MAPPER.writer(schema);
283+
FloatArray input = new FloatArray(-0.5f, 89245.25f, 55555555.5f);
284+
byte[] bytes = w.writeValueAsBytes(input);
285+
assertEquals(14, bytes.length);
286+
287+
FloatArray result = MAPPER.readerFor(FloatArray.class).with(schema)
288+
.readValue(bytes);
289+
_assertEquals(input.values, result.values);
290+
}
291+
292+
private void _assertEquals(float[] exp, float[] act)
293+
{
294+
assertEquals(exp.length, act.length);
295+
for (int i = 0; i < exp.length; ++i) {
296+
// note: caller ensures it only uses values that reliably round-trip
297+
if (exp[i] != act[i]) {
298+
fail("Entry #"+i+" wrong: expected "+exp[i]+", got "+act[i]);
299+
}
300+
}
301+
}
302+
240303
}

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Modules:
1414
2.8.9 (not yet released)
1515

1616
#72: (proto) parser fails with /* comment */
17+
#85: _decode32Bits() bug in ProtobufParser
18+
(reported by marsqing@github)
1719

1820
2.8.8 (05-Apr-2017)
1921

0 commit comments

Comments
 (0)