Skip to content

Commit

Permalink
One minor change wrt #257 to allow zero-length BigInteger represent…
Browse files Browse the repository at this point in the history
…ation for BigInteger.ZERO
  • Loading branch information
cowtowncoder committed Mar 19, 2021
1 parent aeac2f1 commit 467b8ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2101,11 +2101,12 @@ private final int _fourBytesToIntSlow() throws IOException
private final void _finishBigInteger() throws IOException
{
byte[] raw = _read7BitBinaryWithLength();
// [dataformats-binary#257]: 0-length special case to handle
if (raw.length == 0) {
// [dataformats-binary#257]: illegal to have 0-length contents
_reportError("Invalid encoding of `BigInteger`: length 0");
_numberBigInt = BigInteger.ZERO;
} else {
_numberBigInt = new BigInteger(raw);
}
_numberBigInt = new BigInteger(raw);
_numTypesValid = NR_BIGINT;
_numberType = NumberType.BIG_INTEGER;
}
Expand Down Expand Up @@ -2147,11 +2148,13 @@ private final void _finishBigDecimal() throws IOException
{
int scale = SmileUtil.zigzagDecode(_readUnsignedVInt());
byte[] raw = _read7BitBinaryWithLength();
// [dataformats-binary#257]: 0-length special case to handle
if (raw.length == 0) {
// [dataformats-binary#257]: illegal to have 0-length contents
_reportError("Invalid encoding of `BigDecimal` value: length 0");
_numberBigDecimal = BigDecimal.ZERO;
} else {
BigInteger unscaledValue = new BigInteger(raw);
_numberBigDecimal = new BigDecimal(unscaledValue, scale);
}
_numberBigDecimal = new BigDecimal(new BigInteger(raw), scale);
_numTypesValid = NR_BIGDECIMAL;
_numberType = NumberType.BIG_DECIMAL;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.fasterxml.jackson.dataformat.smile.fuzz;

import com.fasterxml.jackson.core.exc.StreamReadException;
import java.math.BigDecimal;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

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

0 comments on commit 467b8ce

Please sign in to comment.