Skip to content

Commit

Permalink
Handle invalid BJData optimized type, fix nlohmann#3461
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed May 1, 2022
1 parent 5352856 commit 1565f1e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/nlohmann/detail/input/binary_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2178,6 +2178,13 @@ class binary_reader
std::vector<char_int_type> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type

result.second = get(); // must not ignore 'N', because 'N' maybe the type
if (JSON_HEDLEY_UNLIKELY( input_format == input_format_t::bjdata && std::find(bjdx.begin(), bjdx.end(), result.second) != bjdx.end() ))
{
auto last_token = get_token_string();
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
}

if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type") || (input_format == input_format_t::bjdata && std::find(bjdx.begin(), bjdx.end(), result.second) != bjdx.end() )))
{
return false;
Expand Down
7 changes: 7 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10645,6 +10645,13 @@ class binary_reader
std::vector<char_int_type> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type

result.second = get(); // must not ignore 'N', because 'N' maybe the type
if (JSON_HEDLEY_UNLIKELY( input_format == input_format_t::bjdata && std::find(bjdx.begin(), bjdx.end(), result.second) != bjdx.end() ))
{
auto last_token = get_token_string();
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
}

if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type") || (input_format == input_format_t::bjdata && std::find(bjdx.begin(), bjdx.end(), result.second) != bjdx.end() )))
{
return false;
Expand Down
5 changes: 5 additions & 0 deletions test/src/unit-bjdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2626,6 +2626,11 @@ TEST_CASE("BJData")
CHECK_THROWS_AS(_ = json::from_bjdata(vU), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_bjdata(vU), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing BJData value: unexpected end of input");
CHECK(json::from_bjdata(vU, true, false).is_discarded());

std::vector<uint8_t> v1 = {'[', '$', '['};
CHECK_THROWS_AS(_ = json::from_bjdata(v1), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_bjdata(v1), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x5B is not a permitted optimized array type");
CHECK(json::from_bjdata(v1, true, false).is_discarded());
}

SECTION("arrays")
Expand Down

0 comments on commit 1565f1e

Please sign in to comment.