diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 3f82a5703..397683744 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -811,25 +811,37 @@ class binary_reader case 0xD8: { std::uint8_t subtype_to_ignore{}; - get_number(input_format_t::cbor, subtype_to_ignore); + if (!get_number(input_format_t::cbor, subtype_to_ignore)) + { + return false; + } break; } case 0xD9: { std::uint16_t subtype_to_ignore{}; - get_number(input_format_t::cbor, subtype_to_ignore); + if (!get_number(input_format_t::cbor, subtype_to_ignore)) + { + return false; + } break; } case 0xDA: { std::uint32_t subtype_to_ignore{}; - get_number(input_format_t::cbor, subtype_to_ignore); + if (!get_number(input_format_t::cbor, subtype_to_ignore)) + { + return false; + } break; } case 0xDB: { std::uint64_t subtype_to_ignore{}; - get_number(input_format_t::cbor, subtype_to_ignore); + if (!get_number(input_format_t::cbor, subtype_to_ignore)) + { + return false; + } break; } default: @@ -847,28 +859,40 @@ class binary_reader case 0xD8: { std::uint8_t subtype{}; - get_number(input_format_t::cbor, subtype); + if (!get_number(input_format_t::cbor, subtype)) + { + return false; + } b.set_subtype(detail::conditional_static_cast(subtype)); break; } case 0xD9: { std::uint16_t subtype{}; - get_number(input_format_t::cbor, subtype); + if (!get_number(input_format_t::cbor, subtype)) + { + return false; + } b.set_subtype(detail::conditional_static_cast(subtype)); break; } case 0xDA: { std::uint32_t subtype{}; - get_number(input_format_t::cbor, subtype); + if (!get_number(input_format_t::cbor, subtype)) + { + return false; + } b.set_subtype(detail::conditional_static_cast(subtype)); break; } case 0xDB: { std::uint64_t subtype{}; - get_number(input_format_t::cbor, subtype); + if (!get_number(input_format_t::cbor, subtype)) + { + return false; + } b.set_subtype(detail::conditional_static_cast(subtype)); break; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 17fea3abf..124b5a9da 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -11360,25 +11360,37 @@ class binary_reader case 0xD8: { std::uint8_t subtype_to_ignore{}; - get_number(input_format_t::cbor, subtype_to_ignore); + if (!get_number(input_format_t::cbor, subtype_to_ignore)) + { + return false; + } break; } case 0xD9: { std::uint16_t subtype_to_ignore{}; - get_number(input_format_t::cbor, subtype_to_ignore); + if (!get_number(input_format_t::cbor, subtype_to_ignore)) + { + return false; + } break; } case 0xDA: { std::uint32_t subtype_to_ignore{}; - get_number(input_format_t::cbor, subtype_to_ignore); + if (!get_number(input_format_t::cbor, subtype_to_ignore)) + { + return false; + } break; } case 0xDB: { std::uint64_t subtype_to_ignore{}; - get_number(input_format_t::cbor, subtype_to_ignore); + if (!get_number(input_format_t::cbor, subtype_to_ignore)) + { + return false; + } break; } default: @@ -11396,28 +11408,40 @@ class binary_reader case 0xD8: { std::uint8_t subtype{}; - get_number(input_format_t::cbor, subtype); + if (!get_number(input_format_t::cbor, subtype)) + { + return false; + } b.set_subtype(detail::conditional_static_cast(subtype)); break; } case 0xD9: { std::uint16_t subtype{}; - get_number(input_format_t::cbor, subtype); + if (!get_number(input_format_t::cbor, subtype)) + { + return false; + } b.set_subtype(detail::conditional_static_cast(subtype)); break; } case 0xDA: { std::uint32_t subtype{}; - get_number(input_format_t::cbor, subtype); + if (!get_number(input_format_t::cbor, subtype)) + { + return false; + } b.set_subtype(detail::conditional_static_cast(subtype)); break; } case 0xDB: { std::uint64_t subtype{}; - get_number(input_format_t::cbor, subtype); + if (!get_number(input_format_t::cbor, subtype)) + { + return false; + } b.set_subtype(detail::conditional_static_cast(subtype)); break; } diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp index 7ae4170d3..29b52d701 100644 --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -1530,4 +1530,29 @@ TEST_CASE("issue #4320 - custom base class must not leak nlohmann::detail into A CHECK(j == json({{"x", 1.0}, {"y", 2.0}, {"z", 3.0}})); } +TEST_CASE("issue #5338 - truncated CBOR tagged binary subtype is rejected") +{ + const std::vector> truncated_tags = + { + {0xD8}, + {0xD9, 0x00}, + {0xDA, 0x00, 0x00, 0x00}, + {0xDB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} + }; + + for (const auto& data : truncated_tags) + { + CAPTURE(data); + for (const auto tag_handler : + { + json::cbor_tag_handler_t::ignore, json::cbor_tag_handler_t::store + }) + { + CAPTURE(tag_handler); + const auto result = json::from_cbor(data, true, false, tag_handler); + CHECK(result.is_discarded()); + } + } +} + DOCTEST_CLANG_SUPPRESS_WARNING_POP