Skip to content

Commit 96e9fb4

Browse files
committed
Fix CBOR tag handlers not recognizing tags 0-5 and 21-23
The tagged-item switch in binary_reader::parse_cbor_internal() only handled head bytes 0xC6-0xD4 and 0xD8-0xDB. Bytes 0xC0-0xC5 (tags 0-5: date/time, epoch, bignum, decimal, bigfloat) and 0xD5-0xD7 (tags 21-23: base64url, base64, base16 conversion hints) fell through to the default case and were reported as invalid bytes, even under cbor_tag_handler_t::ignore and ::store, despite being valid CBOR major-type-6 tags per RFC 8949. Add the missing case labels so the full 0xC0-0xDB range is handled uniformly. Extend the "Tagged values" test in unit-cbor.cpp to cover 0xC0-0xD7, and update the CBOR docs to state the corrected tag range. Fixes #5315
1 parent 2e23687 commit 96e9fb4

4 files changed

Lines changed: 25 additions & 5 deletions

File tree

docs/mkdocs/docs/features/binary_formats/cbor.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ The library maps CBOR types to JSON value types as follows:
181181

182182
!!! warning "Tagged items"
183183

184-
Tagged items will throw a parse error by default. They can be ignored by passing `cbor_tag_handler_t::ignore` to function `from_cbor`. They can be stored by passing `cbor_tag_handler_t::store` to function `from_cbor`.
184+
Tagged items (`0xC0`..`0xDB`) will throw a parse error by default. They can be ignored by passing `cbor_tag_handler_t::ignore` to function `from_cbor`. They can be stored by passing `cbor_tag_handler_t::store` to function `from_cbor`.
185185

186186
??? example
187187

include/nlohmann/detail/input/binary_reader.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,13 @@ class binary_reader
760760
case 0xBF: // map (indefinite length)
761761
return get_cbor_object(detail::unknown_size(), tag_handler);
762762

763-
case 0xC6: // tagged item
763+
case 0xC0: // tagged item
764+
case 0xC1:
765+
case 0xC2:
766+
case 0xC3:
767+
case 0xC4:
768+
case 0xC5:
769+
case 0xC6:
764770
case 0xC7:
765771
case 0xC8:
766772
case 0xC9:
@@ -775,6 +781,9 @@ class binary_reader
775781
case 0xD2:
776782
case 0xD3:
777783
case 0xD4:
784+
case 0xD5:
785+
case 0xD6:
786+
case 0xD7:
778787
case 0xD8: // tagged item (1 byte follows)
779788
case 0xD9: // tagged item (2 bytes follow)
780789
case 0xDA: // tagged item (4 bytes follow)

single_include/nlohmann/json.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11309,7 +11309,13 @@ class binary_reader
1130911309
case 0xBF: // map (indefinite length)
1131011310
return get_cbor_object(detail::unknown_size(), tag_handler);
1131111311

11312-
case 0xC6: // tagged item
11312+
case 0xC0: // tagged item
11313+
case 0xC1:
11314+
case 0xC2:
11315+
case 0xC3:
11316+
case 0xC4:
11317+
case 0xC5:
11318+
case 0xC6:
1131311319
case 0xC7:
1131411320
case 0xC8:
1131511321
case 0xC9:
@@ -11324,6 +11330,9 @@ class binary_reader
1132411330
case 0xD2:
1132511331
case 0xD3:
1132611332
case 0xD4:
11333+
case 0xD5:
11334+
case 0xD6:
11335+
case 0xD7:
1132711336
case 0xD8: // tagged item (1 byte follows)
1132811337
case 0xD9: // tagged item (2 bytes follow)
1132911338
case 0xDA: // tagged item (4 bytes follow)

tests/src/unit-cbor.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,11 +2529,13 @@ TEST_CASE("Tagged values")
25292529
const json j = "s";
25302530
auto v = json::to_cbor(j);
25312531

2532-
SECTION("0xC6..0xD4")
2532+
SECTION("0xC0..0xD7")
25332533
{
25342534
for (const auto b : std::vector<std::uint8_t>
25352535
{
2536-
0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4
2536+
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5,
2537+
0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4,
2538+
0xD5, 0xD6, 0xD7
25372539
})
25382540
{
25392541
CAPTURE(b);

0 commit comments

Comments
 (0)