Skip to content

Commit

Permalink
Discard optimized containers with negative counts in UBJSON/BJData (#…
Browse files Browse the repository at this point in the history
…3491,#3492,#3490) (#3500)

* Discard optimized containers with negative counts in UBJSON/BJData (#3491,#3492,#3490)

* fix msvc error

* update unit tests for negative sized containers

* use a loop to test 0 ndarray dimension

* throw an error when count is negative, merge CHECK_THROW_AS and _WITH with _WITH_AS
  • Loading branch information
fangq committed May 18, 2022
1 parent 6a73920 commit 93c9e0c
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 143 deletions.
29 changes: 29 additions & 0 deletions include/nlohmann/detail/input/binary_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2020,6 +2020,11 @@ class binary_reader
{
return false;
}
if (number < 0)
{
return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
}
result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
return true;
}
Expand All @@ -2031,6 +2036,11 @@ class binary_reader
{
return false;
}
if (number < 0)
{
return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
}
result = static_cast<std::size_t>(number);
return true;
}
Expand All @@ -2042,6 +2052,11 @@ class binary_reader
{
return false;
}
if (number < 0)
{
return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
}
result = static_cast<std::size_t>(number);
return true;
}
Expand All @@ -2053,6 +2068,11 @@ class binary_reader
{
return false;
}
if (number < 0)
{
return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
}
result = static_cast<std::size_t>(number);
return true;
}
Expand Down Expand Up @@ -2120,6 +2140,15 @@ class binary_reader
}
if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
{
for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
{
if ( i == 0 )
{
result = 0;
return true;
}
}

string_t key = "_ArraySize_";
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
{
Expand Down
29 changes: 29 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10494,6 +10494,11 @@ class binary_reader
{
return false;
}
if (number < 0)
{
return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
}
result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
return true;
}
Expand All @@ -10505,6 +10510,11 @@ class binary_reader
{
return false;
}
if (number < 0)
{
return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
}
result = static_cast<std::size_t>(number);
return true;
}
Expand All @@ -10516,6 +10526,11 @@ class binary_reader
{
return false;
}
if (number < 0)
{
return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
}
result = static_cast<std::size_t>(number);
return true;
}
Expand All @@ -10527,6 +10542,11 @@ class binary_reader
{
return false;
}
if (number < 0)
{
return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
}
result = static_cast<std::size_t>(number);
return true;
}
Expand Down Expand Up @@ -10594,6 +10614,15 @@ class binary_reader
}
if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
{
for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
{
if ( i == 0 )
{
result = 0;
return true;
}
}

string_t key = "_ArraySize_";
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
{
Expand Down
Loading

0 comments on commit 93c9e0c

Please sign in to comment.