Skip to content

Commit 4f64d8d

Browse files
authored
Modernize integer comparison (#4577)
Replace static_cast<size_t>(-1) with std::numeric_limits<std::size_t>::max() via the detail::unknown_size() function
1 parent 2134cb9 commit 4f64d8d

File tree

5 files changed

+52
-42
lines changed

5 files changed

+52
-42
lines changed

include/nlohmann/detail/input/binary_reader.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class binary_reader
172172
std::int32_t document_size{};
173173
get_number<std::int32_t, true>(input_format_t::bson, document_size);
174174

175-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
175+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
176176
{
177177
return false;
178178
}
@@ -394,7 +394,7 @@ class binary_reader
394394
std::int32_t document_size{};
395395
get_number<std::int32_t, true>(input_format_t::bson, document_size);
396396

397-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
397+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
398398
{
399399
return false;
400400
}
@@ -654,7 +654,7 @@ class binary_reader
654654
}
655655

656656
case 0x9F: // array (indefinite length)
657-
return get_cbor_array(static_cast<std::size_t>(-1), tag_handler);
657+
return get_cbor_array(detail::unknown_size(), tag_handler);
658658

659659
// map (0x00..0x17 pairs of data items follow)
660660
case 0xA0:
@@ -708,7 +708,7 @@ class binary_reader
708708
}
709709

710710
case 0xBF: // map (indefinite length)
711-
return get_cbor_object(static_cast<std::size_t>(-1), tag_handler);
711+
return get_cbor_object(detail::unknown_size(), tag_handler);
712712

713713
case 0xC6: // tagged item
714714
case 0xC7:
@@ -1096,7 +1096,7 @@ class binary_reader
10961096
}
10971097

10981098
/*!
1099-
@param[in] len the length of the array or static_cast<std::size_t>(-1) for an
1099+
@param[in] len the length of the array or detail::unknown_size() for an
11001100
array of indefinite size
11011101
@param[in] tag_handler how CBOR tags should be treated
11021102
@return whether array creation completed
@@ -1109,7 +1109,7 @@ class binary_reader
11091109
return false;
11101110
}
11111111

1112-
if (len != static_cast<std::size_t>(-1))
1112+
if (len != detail::unknown_size())
11131113
{
11141114
for (std::size_t i = 0; i < len; ++i)
11151115
{
@@ -1134,7 +1134,7 @@ class binary_reader
11341134
}
11351135

11361136
/*!
1137-
@param[in] len the length of the object or static_cast<std::size_t>(-1) for an
1137+
@param[in] len the length of the object or detail::unknown_size() for an
11381138
object of indefinite size
11391139
@param[in] tag_handler how CBOR tags should be treated
11401140
@return whether object creation completed
@@ -1150,7 +1150,7 @@ class binary_reader
11501150
if (len != 0)
11511151
{
11521152
string_t key;
1153-
if (len != static_cast<std::size_t>(-1))
1153+
if (len != detail::unknown_size())
11541154
{
11551155
for (std::size_t i = 0; i < len; ++i)
11561156
{
@@ -2568,7 +2568,7 @@ class binary_reader
25682568
}
25692569
else
25702570
{
2571-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
2571+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
25722572
{
25732573
return false;
25742574
}
@@ -2646,7 +2646,7 @@ class binary_reader
26462646
}
26472647
else
26482648
{
2649-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
2649+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
26502650
{
26512651
return false;
26522652
}
@@ -2982,7 +2982,7 @@ class binary_reader
29822982
}
29832983

29842984
private:
2985-
static JSON_INLINE_VARIABLE constexpr std::size_t npos = static_cast<std::size_t>(-1);
2985+
static JSON_INLINE_VARIABLE constexpr std::size_t npos = detail::unknown_size();
29862986

29872987
/// input adapter
29882988
InputAdapterType ia;

include/nlohmann/detail/input/json_sax.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ struct json_sax
145145

146146
namespace detail
147147
{
148+
constexpr std::size_t unknown_size()
149+
{
150+
return (std::numeric_limits<std::size_t>::max)();
151+
}
152+
148153
/*!
149154
@brief SAX implementation to create a JSON value from SAX events
150155
@@ -242,7 +247,7 @@ class json_sax_dom_parser
242247
}
243248
#endif
244249

245-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
250+
if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
246251
{
247252
JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
248253
}
@@ -291,7 +296,7 @@ class json_sax_dom_parser
291296
}
292297
#endif
293298

294-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
299+
if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
295300
{
296301
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
297302
}
@@ -559,7 +564,7 @@ class json_sax_dom_callback_parser
559564
#endif
560565

561566
// check object limit
562-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
567+
if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
563568
{
564569
JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
565570
}
@@ -657,7 +662,7 @@ class json_sax_dom_callback_parser
657662
#endif
658663

659664
// check array limit
660-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
665+
if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
661666
{
662667
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
663668
}
@@ -946,7 +951,7 @@ class json_sax_acceptor
946951
return true;
947952
}
948953

949-
bool start_object(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
954+
bool start_object(std::size_t /*unused*/ = detail::unknown_size())
950955
{
951956
return true;
952957
}
@@ -961,7 +966,7 @@ class json_sax_acceptor
961966
return true;
962967
}
963968

964-
bool start_array(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
969+
bool start_array(std::size_t /*unused*/ = detail::unknown_size())
965970
{
966971
return true;
967972
}

include/nlohmann/detail/input/parser.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class parser
194194
{
195195
case token_type::begin_object:
196196
{
197-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
197+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
198198
{
199199
return false;
200200
}
@@ -239,7 +239,7 @@ class parser
239239

240240
case token_type::begin_array:
241241
{
242-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
242+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
243243
{
244244
return false;
245245
}

include/nlohmann/json.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,10 +751,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
751751
return it;
752752
}
753753

754-
reference set_parent(reference j, std::size_t old_capacity = static_cast<std::size_t>(-1))
754+
reference set_parent(reference j, std::size_t old_capacity = detail::unknown_size())
755755
{
756756
#if JSON_DIAGNOSTICS
757-
if (old_capacity != static_cast<std::size_t>(-1))
757+
if (old_capacity != detail::unknown_size())
758758
{
759759
// see https://github.com/nlohmann/json/issues/2838
760760
JSON_ASSERT(type() == value_t::array);

single_include/nlohmann/json.hpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8677,6 +8677,11 @@ struct json_sax
86778677

86788678
namespace detail
86798679
{
8680+
constexpr std::size_t unknown_size()
8681+
{
8682+
return (std::numeric_limits<std::size_t>::max)();
8683+
}
8684+
86808685
/*!
86818686
@brief SAX implementation to create a JSON value from SAX events
86828687

@@ -8774,7 +8779,7 @@ class json_sax_dom_parser
87748779
}
87758780
#endif
87768781

8777-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
8782+
if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
87788783
{
87798784
JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
87808785
}
@@ -8823,7 +8828,7 @@ class json_sax_dom_parser
88238828
}
88248829
#endif
88258830

8826-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
8831+
if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
88278832
{
88288833
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
88298834
}
@@ -9091,7 +9096,7 @@ class json_sax_dom_callback_parser
90919096
#endif
90929097

90939098
// check object limit
9094-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
9099+
if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
90959100
{
90969101
JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
90979102
}
@@ -9189,7 +9194,7 @@ class json_sax_dom_callback_parser
91899194
#endif
91909195

91919196
// check array limit
9192-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
9197+
if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
91939198
{
91949199
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
91959200
}
@@ -9478,7 +9483,7 @@ class json_sax_acceptor
94789483
return true;
94799484
}
94809485

9481-
bool start_object(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
9486+
bool start_object(std::size_t /*unused*/ = detail::unknown_size())
94829487
{
94839488
return true;
94849489
}
@@ -9493,7 +9498,7 @@ class json_sax_acceptor
94939498
return true;
94949499
}
94959500

9496-
bool start_array(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
9501+
bool start_array(std::size_t /*unused*/ = detail::unknown_size())
94979502
{
94989503
return true;
94999504
}
@@ -9825,7 +9830,7 @@ class binary_reader
98259830
std::int32_t document_size{};
98269831
get_number<std::int32_t, true>(input_format_t::bson, document_size);
98279832

9828-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
9833+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
98299834
{
98309835
return false;
98319836
}
@@ -10047,7 +10052,7 @@ class binary_reader
1004710052
std::int32_t document_size{};
1004810053
get_number<std::int32_t, true>(input_format_t::bson, document_size);
1004910054

10050-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
10055+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
1005110056
{
1005210057
return false;
1005310058
}
@@ -10307,7 +10312,7 @@ class binary_reader
1030710312
}
1030810313

1030910314
case 0x9F: // array (indefinite length)
10310-
return get_cbor_array(static_cast<std::size_t>(-1), tag_handler);
10315+
return get_cbor_array(detail::unknown_size(), tag_handler);
1031110316

1031210317
// map (0x00..0x17 pairs of data items follow)
1031310318
case 0xA0:
@@ -10361,7 +10366,7 @@ class binary_reader
1036110366
}
1036210367

1036310368
case 0xBF: // map (indefinite length)
10364-
return get_cbor_object(static_cast<std::size_t>(-1), tag_handler);
10369+
return get_cbor_object(detail::unknown_size(), tag_handler);
1036510370

1036610371
case 0xC6: // tagged item
1036710372
case 0xC7:
@@ -10749,7 +10754,7 @@ class binary_reader
1074910754
}
1075010755

1075110756
/*!
10752-
@param[in] len the length of the array or static_cast<std::size_t>(-1) for an
10757+
@param[in] len the length of the array or detail::unknown_size() for an
1075310758
array of indefinite size
1075410759
@param[in] tag_handler how CBOR tags should be treated
1075510760
@return whether array creation completed
@@ -10762,7 +10767,7 @@ class binary_reader
1076210767
return false;
1076310768
}
1076410769

10765-
if (len != static_cast<std::size_t>(-1))
10770+
if (len != detail::unknown_size())
1076610771
{
1076710772
for (std::size_t i = 0; i < len; ++i)
1076810773
{
@@ -10787,7 +10792,7 @@ class binary_reader
1078710792
}
1078810793

1078910794
/*!
10790-
@param[in] len the length of the object or static_cast<std::size_t>(-1) for an
10795+
@param[in] len the length of the object or detail::unknown_size() for an
1079110796
object of indefinite size
1079210797
@param[in] tag_handler how CBOR tags should be treated
1079310798
@return whether object creation completed
@@ -10803,7 +10808,7 @@ class binary_reader
1080310808
if (len != 0)
1080410809
{
1080510810
string_t key;
10806-
if (len != static_cast<std::size_t>(-1))
10811+
if (len != detail::unknown_size())
1080710812
{
1080810813
for (std::size_t i = 0; i < len; ++i)
1080910814
{
@@ -12221,7 +12226,7 @@ class binary_reader
1222112226
}
1222212227
else
1222312228
{
12224-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
12229+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
1222512230
{
1222612231
return false;
1222712232
}
@@ -12299,7 +12304,7 @@ class binary_reader
1229912304
}
1230012305
else
1230112306
{
12302-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
12307+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
1230312308
{
1230412309
return false;
1230512310
}
@@ -12635,7 +12640,7 @@ class binary_reader
1263512640
}
1263612641

1263712642
private:
12638-
static JSON_INLINE_VARIABLE constexpr std::size_t npos = static_cast<std::size_t>(-1);
12643+
static JSON_INLINE_VARIABLE constexpr std::size_t npos = detail::unknown_size();
1263912644

1264012645
/// input adapter
1264112646
InputAdapterType ia;
@@ -12905,7 +12910,7 @@ class parser
1290512910
{
1290612911
case token_type::begin_object:
1290712912
{
12908-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
12913+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
1290912914
{
1291012915
return false;
1291112916
}
@@ -12950,7 +12955,7 @@ class parser
1295012955

1295112956
case token_type::begin_array:
1295212957
{
12953-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
12958+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
1295412959
{
1295512960
return false;
1295612961
}
@@ -20606,10 +20611,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
2060620611
return it;
2060720612
}
2060820613

20609-
reference set_parent(reference j, std::size_t old_capacity = static_cast<std::size_t>(-1))
20614+
reference set_parent(reference j, std::size_t old_capacity = detail::unknown_size())
2061020615
{
2061120616
#if JSON_DIAGNOSTICS
20612-
if (old_capacity != static_cast<std::size_t>(-1))
20617+
if (old_capacity != detail::unknown_size())
2061320618
{
2061420619
// see https://github.com/nlohmann/json/issues/2838
2061520620
JSON_ASSERT(type() == value_t::array);

0 commit comments

Comments
 (0)