Skip to content

Commit 467b8ce

Browse files
committed
One minor change wrt #257 to allow zero-length BigInteger representation for BigInteger.ZERO
1 parent aeac2f1 commit 467b8ce

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileParser.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,11 +2101,12 @@ private final int _fourBytesToIntSlow() throws IOException
21012101
private final void _finishBigInteger() throws IOException
21022102
{
21032103
byte[] raw = _read7BitBinaryWithLength();
2104+
// [dataformats-binary#257]: 0-length special case to handle
21042105
if (raw.length == 0) {
2105-
// [dataformats-binary#257]: illegal to have 0-length contents
2106-
_reportError("Invalid encoding of `BigInteger`: length 0");
2106+
_numberBigInt = BigInteger.ZERO;
2107+
} else {
2108+
_numberBigInt = new BigInteger(raw);
21072109
}
2108-
_numberBigInt = new BigInteger(raw);
21092110
_numTypesValid = NR_BIGINT;
21102111
_numberType = NumberType.BIG_INTEGER;
21112112
}
@@ -2147,11 +2148,13 @@ private final void _finishBigDecimal() throws IOException
21472148
{
21482149
int scale = SmileUtil.zigzagDecode(_readUnsignedVInt());
21492150
byte[] raw = _read7BitBinaryWithLength();
2151+
// [dataformats-binary#257]: 0-length special case to handle
21502152
if (raw.length == 0) {
2151-
// [dataformats-binary#257]: illegal to have 0-length contents
2152-
_reportError("Invalid encoding of `BigDecimal` value: length 0");
2153+
_numberBigDecimal = BigDecimal.ZERO;
2154+
} else {
2155+
BigInteger unscaledValue = new BigInteger(raw);
2156+
_numberBigDecimal = new BigDecimal(unscaledValue, scale);
21532157
}
2154-
_numberBigDecimal = new BigDecimal(new BigInteger(raw), scale);
21552158
_numTypesValid = NR_BIGDECIMAL;
21562159
_numberType = NumberType.BIG_DECIMAL;
21572160
}

smile/src/test/java/com/fasterxml/jackson/dataformat/smile/fuzz/Fuzz32168BigDecimalTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.fasterxml.jackson.dataformat.smile.fuzz;
22

3-
import com.fasterxml.jackson.core.exc.StreamReadException;
3+
import java.math.BigDecimal;
44

5+
import com.fasterxml.jackson.databind.JsonNode;
56
import com.fasterxml.jackson.databind.ObjectMapper;
67

78
import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile;
@@ -20,11 +21,9 @@ public void testInvalidBigDecimal() throws Exception
2021
(byte) 0xBF, // scale: -32
2122
(byte) 0x80 // length: 0 (invalid
2223
};
23-
try {
24-
/*JsonNode root =*/ MAPPER.readTree(input);
25-
fail("Should not pass");
26-
} catch (StreamReadException e) {
27-
verifyException(e, "Invalid encoding of `BigDecimal` value: length 0");
28-
}
24+
JsonNode root = MAPPER.readTree(input);
25+
assertTrue(root.isNumber());
26+
assertTrue(root.isBigDecimal());
27+
assertEquals(BigDecimal.ZERO, root.decimalValue());
2928
}
3029
}

0 commit comments

Comments
 (0)