Skip to content

Commit 3a65cc4

Browse files
committed
Fix nlohmann#3513, explain is_ndarray flag
1 parent 6b97599 commit 3a65cc4

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

include/nlohmann/detail/input/binary_reader.hpp

+16-12
Original file line numberDiff line numberDiff line change
@@ -1938,9 +1938,9 @@ class binary_reader
19381938
{
19391939
std::pair<std::size_t, char_int_type> size_and_type;
19401940
size_t dimlen = 0;
1941-
bool inside_ndarray = true;
1941+
bool no_ndarray = true;
19421942

1943-
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, inside_ndarray)))
1943+
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
19441944
{
19451945
return false;
19461946
}
@@ -1953,7 +1953,7 @@ class binary_reader
19531953
{
19541954
for (std::size_t i = 0; i < size_and_type.first; ++i)
19551955
{
1956-
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, inside_ndarray, size_and_type.second)))
1956+
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
19571957
{
19581958
return false;
19591959
}
@@ -1965,7 +1965,7 @@ class binary_reader
19651965
{
19661966
for (std::size_t i = 0; i < size_and_type.first; ++i)
19671967
{
1968-
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, inside_ndarray)))
1968+
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
19691969
{
19701970
return false;
19711971
}
@@ -1977,7 +1977,7 @@ class binary_reader
19771977
{
19781978
while (current != ']')
19791979
{
1980-
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, inside_ndarray, current)))
1980+
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
19811981
{
19821982
return false;
19831983
}
@@ -1990,12 +1990,16 @@ class binary_reader
19901990

19911991
/*!
19921992
@param[out] result determined size
1993-
@param[in,out] inside_ndarray whether the parser is parsing an ND array dimensional vector
1993+
@param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
1994+
or ndarray dimension is not allowed; `false` means ndarray
1995+
is allowed; for output, `true` means an ndarray is found;
1996+
is_ndarray can only return `true` when its initial value
1997+
is `false`
19941998
@param[in] prefix type marker if already read, otherwise set to 0
19951999
19962000
@return whether size determination completed
19972001
*/
1998-
bool get_ubjson_size_value(std::size_t& result, bool& inside_ndarray, char_int_type prefix = 0)
2002+
bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
19992003
{
20002004
if (prefix == 0)
20012005
{
@@ -2130,9 +2134,9 @@ class binary_reader
21302134
{
21312135
break;
21322136
}
2133-
if (inside_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
2137+
if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
21342138
{
2135-
return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, exception_message(input_format, "ndarray dimention vector can only contain integers", "size"), nullptr));
2139+
return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, exception_message(input_format, "ndarray dimentional vector is not allowed", "size"), nullptr));
21362140
}
21372141
std::vector<size_t> dim;
21382142
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
@@ -2169,7 +2173,7 @@ class binary_reader
21692173
return false;
21702174
}
21712175
}
2172-
inside_ndarray = true;
2176+
is_ndarray = true;
21732177
return sax->end_array();
21742178
}
21752179
result = 0;
@@ -2650,8 +2654,8 @@ class binary_reader
26502654
{
26512655
// get size of following number string
26522656
std::size_t size{};
2653-
bool inside_ndarray = false;
2654-
auto res = get_ubjson_size_value(size, inside_ndarray);
2657+
bool no_ndarray = true;
2658+
auto res = get_ubjson_size_value(size, no_ndarray);
26552659
if (JSON_HEDLEY_UNLIKELY(!res))
26562660
{
26572661
return res;

single_include/nlohmann/json.hpp

+16-12
Original file line numberDiff line numberDiff line change
@@ -10528,9 +10528,9 @@ class binary_reader
1052810528
{
1052910529
std::pair<std::size_t, char_int_type> size_and_type;
1053010530
size_t dimlen = 0;
10531-
bool inside_ndarray = true;
10531+
bool no_ndarray = true;
1053210532

10533-
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, inside_ndarray)))
10533+
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
1053410534
{
1053510535
return false;
1053610536
}
@@ -10543,7 +10543,7 @@ class binary_reader
1054310543
{
1054410544
for (std::size_t i = 0; i < size_and_type.first; ++i)
1054510545
{
10546-
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, inside_ndarray, size_and_type.second)))
10546+
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
1054710547
{
1054810548
return false;
1054910549
}
@@ -10555,7 +10555,7 @@ class binary_reader
1055510555
{
1055610556
for (std::size_t i = 0; i < size_and_type.first; ++i)
1055710557
{
10558-
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, inside_ndarray)))
10558+
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
1055910559
{
1056010560
return false;
1056110561
}
@@ -10567,7 +10567,7 @@ class binary_reader
1056710567
{
1056810568
while (current != ']')
1056910569
{
10570-
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, inside_ndarray, current)))
10570+
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
1057110571
{
1057210572
return false;
1057310573
}
@@ -10580,12 +10580,16 @@ class binary_reader
1058010580

1058110581
/*!
1058210582
@param[out] result determined size
10583-
@param[in,out] inside_ndarray whether the parser is parsing an ND array dimensional vector
10583+
@param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
10584+
or ndarray dimension is not allowed; `false` means ndarray
10585+
is allowed; for output, `true` means an ndarray is found;
10586+
is_ndarray can only return `true` when its initial value
10587+
is `false`
1058410588
@param[in] prefix type marker if already read, otherwise set to 0
1058510589

1058610590
@return whether size determination completed
1058710591
*/
10588-
bool get_ubjson_size_value(std::size_t& result, bool& inside_ndarray, char_int_type prefix = 0)
10592+
bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
1058910593
{
1059010594
if (prefix == 0)
1059110595
{
@@ -10720,9 +10724,9 @@ class binary_reader
1072010724
{
1072110725
break;
1072210726
}
10723-
if (inside_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
10727+
if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
1072410728
{
10725-
return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, exception_message(input_format, "ndarray dimention vector can only contain integers", "size"), nullptr));
10729+
return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, exception_message(input_format, "ndarray dimentional vector is not allowed", "size"), nullptr));
1072610730
}
1072710731
std::vector<size_t> dim;
1072810732
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
@@ -10759,7 +10763,7 @@ class binary_reader
1075910763
return false;
1076010764
}
1076110765
}
10762-
inside_ndarray = true;
10766+
is_ndarray = true;
1076310767
return sax->end_array();
1076410768
}
1076510769
result = 0;
@@ -11240,8 +11244,8 @@ class binary_reader
1124011244
{
1124111245
// get size of following number string
1124211246
std::size_t size{};
11243-
bool inside_ndarray = false;
11244-
auto res = get_ubjson_size_value(size, inside_ndarray);
11247+
bool no_ndarray = true;
11248+
auto res = get_ubjson_size_value(size, no_ndarray);
1124511249
if (JSON_HEDLEY_UNLIKELY(!res))
1124611250
{
1124711251
return res;

tests/src/unit-bjdata.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -2746,15 +2746,15 @@ TEST_CASE("BJData")
27462746
CHECK(json::from_bjdata(vh, true, false).is_discarded());
27472747

27482748
std::vector<uint8_t> vR = {'[', '$', 'i', '#', '[', 'i', 1, '[', ']', ']', 1};
2749-
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR), "[json.exception.parse_error.113] parse error at byte 8: syntax error while parsing BJData size: ndarray dimention vector can only contain integers", json::parse_error&);
2749+
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR), "[json.exception.parse_error.113] parse error at byte 8: syntax error while parsing BJData size: ndarray dimentional vector is not allowed", json::parse_error&);
27502750
CHECK(json::from_bjdata(vR, true, false).is_discarded());
27512751

27522752
std::vector<uint8_t> vRo = {'[', '$', 'i', '#', '[', 'i', 0, '{', '}', ']', 1};
27532753
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vRo), "[json.exception.parse_error.113] parse error at byte 8: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x7B", json::parse_error&);
27542754
CHECK(json::from_bjdata(vRo, true, false).is_discarded());
27552755

27562756
std::vector<uint8_t> vR1 = {'[', '$', 'i', '#', '[', '[', 'i', 1, ']', ']', 1};
2757-
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR1), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: ndarray dimention vector can only contain integers", json::parse_error&);
2757+
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR1), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: ndarray dimentional vector is not allowed", json::parse_error&);
27582758
CHECK(json::from_bjdata(vR1, true, false).is_discarded());
27592759

27602760
std::vector<uint8_t> vR2 = {'[', '$', 'i', '#', '[', '#', '[', 'i', 1, ']', ']', 1};
@@ -2770,7 +2770,7 @@ TEST_CASE("BJData")
27702770
CHECK(json::from_bjdata(vR4, true, false).is_discarded());
27712771

27722772
std::vector<uint8_t> vR5 = {'[', '$', 'i', '#', '[', '[', '[', ']', ']', ']'};
2773-
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR5), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: ndarray dimention vector can only contain integers", json::parse_error&);
2773+
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR5), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: ndarray dimentional vector is not allowed", json::parse_error&);
27742774
CHECK(json::from_bjdata(vR5, true, false).is_discarded());
27752775

27762776
std::vector<uint8_t> vR6 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};

0 commit comments

Comments
 (0)