2121public class ParserNumbersTest extends CBORTestBase
2222{
2323 private final CBORFactory CBOR_F = cborFactory ();
24-
24+
2525 public void testIntValues () throws Exception
2626 {
2727 // first, single-byte
@@ -101,6 +101,17 @@ public void testInt32Overflow() throws Exception
101101 assertEquals (exp , p .getLongValue ());
102102 assertEquals (NumberType .LONG , p .getNumberType ());
103103 p .close ();
104+
105+ // and, combined, a negative number where the mantissa overflows a signed int32
106+ input = new byte [] {
107+ (byte ) CBORConstants .PREFIX_TYPE_INT_NEG + 26 , // int32, that is, 4 more bytes
108+ -1 , -1 , -1 , -1
109+ };
110+ p = cborParser (input );
111+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
112+ assertEquals (-1L - 0xFFFFFFFFL , p .getLongValue ());
113+ assertEquals (NumberType .LONG , p .getNumberType ());
114+ p .close ();
104115 }
105116
106117 public void testLongValues () throws Exception
@@ -170,6 +181,21 @@ public void testInt64Overflow() throws Exception
170181 assertEquals (exp , p .getBigIntegerValue ());
171182 assertEquals (NumberType .BIG_INTEGER , p .getNumberType ());
172183 p .close ();
184+
185+ // and, combined, a negative number where the mantissa overflows a signed int32
186+ input = new byte [] {
187+ (byte ) CBORConstants .PREFIX_TYPE_INT_NEG + 27 , // int32, that is, 4 more bytes
188+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1
189+ };
190+ p = cborParser (input );
191+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
192+ exp = BigInteger .valueOf (Long .MAX_VALUE ).shiftLeft (1 )
193+ .add (BigInteger .ONE )
194+ .negate ()
195+ .subtract (BigInteger .ONE );
196+ assertEquals (exp , p .getBigIntegerValue ());
197+ assertEquals (NumberType .BIG_INTEGER , p .getNumberType ());
198+ p .close ();
173199 }
174200
175201 public void testDoubleValues () throws Exception
@@ -198,15 +224,15 @@ private void _verifyDouble(CBORFactory f, double value, boolean isNaN) throws Ex
198224 assertEquals ((float ) value , p .getFloatValue ());
199225
200226 assertNull (p .nextToken ());
201-
227+
202228 // also skip
203229 p = cborParser (f , out .toByteArray ());
204230 assertEquals (JsonToken .VALUE_NUMBER_FLOAT , p .nextToken ());
205231 assertNull (p .nextToken ());
206-
232+
207233 p .close ();
208234 }
209-
235+
210236 public void testFloatValues () throws Exception
211237 {
212238 // first, single-byte
@@ -251,7 +277,7 @@ private void _verifyFloat(CBORFactory f, double value, boolean isNaN) throws Exc
251277 p = cborParser (f , out .toByteArray ());
252278 assertEquals (JsonToken .VALUE_NUMBER_FLOAT , p .nextToken ());
253279 assertNull (p .nextToken ());
254-
280+
255281 p .close ();
256282 }
257283
@@ -263,7 +289,7 @@ private void _verifyHalfFloat(JsonFactory f, int i16, double value) throws IOExc
263289 };
264290
265291 boolean expNaN = Double .isNaN (value ) || Double .isInfinite (value );
266-
292+
267293 JsonParser p = f .createParser (data );
268294 assertEquals (JsonToken .VALUE_NUMBER_FLOAT , p .nextToken ());
269295 assertEquals (expNaN , p .isNaN ());
0 commit comments