@@ -2689,32 +2689,48 @@ TEST_CASE("Tagged values")
26892689
26902690 SECTION (" negative integer overflow" )
26912691 {
2692- // CBOR encodes negative integers as -1 - n, where n is uint64_t.
2693- // When n > INT64_MAX, the result cannot be represented in int64_t.
2694- // The library should reject such values with a parse error.
2692+ // CBOR type 0x3B encodes negative integers as: result = -1 - n
2693+ // where n is an 8-byte uint64_t. Valid range for n is [0, INT64_MAX]
2694+ // which produces results in [INT64_MIN, -1].
2695+ // When n > INT64_MAX, the result exceeds int64_t range and is rejected.
2696+ //
2697+ // Note: result = 0 is not possible with type 0x3B since n is an
2698+ // unsigned integer, so the formula -1 - n always produces a negative result.
2699+
2700+ SECTION (" n = 0 is valid (result = -1)" )
2701+ {
2702+ // n = 0, result = -1 - 0 = -1 (smallest magnitude negative)
2703+ const std::vector<uint8_t > input = {0x3B , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
2704+ const auto result = json::from_cbor (input);
2705+ CHECK (result.is_number_integer ());
2706+ CHECK (result.get <int64_t >() == -1 );
2707+ }
26952708
26962709 SECTION (" n = INT64_MAX is valid (result = INT64_MIN)" )
26972710 {
2698- // n = 0x7FFFFFFFFFFFFFFF (INT64_MAX), result = -1 - INT64_MAX = INT64_MIN
2711+ // n = INT64_MAX (0x7FFFFFFFFFFFFFFF)
2712+ // result = -1 - INT64_MAX = INT64_MIN (-9223372036854775808)
26992713 const std::vector<uint8_t > input = {0x3B , 0x7F , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
27002714 const auto result = json::from_cbor (input);
27012715 CHECK (result.is_number_integer ());
27022716 CHECK (result.get <int64_t >() == (std::numeric_limits<int64_t >::min)());
27032717 }
27042718
2705- SECTION (" n = INT64_MAX + 1 causes overflow" )
2719+ SECTION (" n = INT64_MAX + 1 is rejected ( overflow) " )
27062720 {
2707- // n = 0x8000000000000000 (INT64_MAX + 1), result would be -9223372036854775809
2721+ // n = INT64_MAX + 1 (0x8000000000000000)
2722+ // result = -1 - n = -9223372036854775809, which exceeds int64_t range
27082723 const std::vector<uint8_t > input = {0x3B , 0x80 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
27092724 json _;
27102725 CHECK_THROWS_WITH_AS (_ = json::from_cbor (input),
27112726 " [json.exception.parse_error.112] parse error at byte 9: syntax error while parsing CBOR value: negative integer overflow" ,
27122727 json::parse_error);
27132728 }
27142729
2715- SECTION (" n = UINT64_MAX causes overflow" )
2730+ SECTION (" n = UINT64_MAX is rejected ( overflow) " )
27162731 {
2717- // n = 0xFFFFFFFFFFFFFFFF (UINT64_MAX), result would be -18446744073709551616
2732+ // n = UINT64_MAX (0xFFFFFFFFFFFFFFFF)
2733+ // result = -1 - n = -18446744073709551616, which exceeds int64_t range
27182734 const std::vector<uint8_t > input = {0x3B , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
27192735 json _;
27202736 CHECK_THROWS_WITH_AS (_ = json::from_cbor (input),
0 commit comments