Skip to content

Commit e93a8a4

Browse files
committed
Modernize integer comparison
Replace static_cast<size_t>(-1) with std::numeric_limits<std::size_t>::max()
1 parent 6057b31 commit e93a8a4

File tree

5 files changed

+42
-42
lines changed

5 files changed

+42
-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(std::numeric_limits<std::size_t>::max())))
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(std::numeric_limits<std::size_t>::max())))
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(std::numeric_limits<std::size_t>::max(), 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(std::numeric_limits<std::size_t>::max(), 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 std::numeric_limits<std::size_t>::max() 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 != std::numeric_limits<std::size_t>::max())
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 std::numeric_limits<std::size_t>::max() 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 != std::numeric_limits<std::size_t>::max())
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(std::numeric_limits<std::size_t>::max())))
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(std::numeric_limits<std::size_t>::max())))
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 = std::numeric_limits<std::size_t>::max();
29862986

29872987
/// input adapter
29882988
InputAdapterType ia;

include/nlohmann/detail/input/json_sax.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class json_sax_dom_parser
242242
}
243243
#endif
244244

245-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
245+
if (JSON_HEDLEY_UNLIKELY(len != std::numeric_limits<std::size_t>::max() && len > ref_stack.back()->max_size()))
246246
{
247247
JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
248248
}
@@ -291,7 +291,7 @@ class json_sax_dom_parser
291291
}
292292
#endif
293293

294-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
294+
if (JSON_HEDLEY_UNLIKELY(len != std::numeric_limits<std::size_t>::max() && len > ref_stack.back()->max_size()))
295295
{
296296
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
297297
}
@@ -559,7 +559,7 @@ class json_sax_dom_callback_parser
559559
#endif
560560

561561
// check object limit
562-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
562+
if (JSON_HEDLEY_UNLIKELY(len != std::numeric_limits<std::size_t>::max() && len > ref_stack.back()->max_size()))
563563
{
564564
JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
565565
}
@@ -657,7 +657,7 @@ class json_sax_dom_callback_parser
657657
#endif
658658

659659
// check array limit
660-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
660+
if (JSON_HEDLEY_UNLIKELY(len != std::numeric_limits<std::size_t>::max() && len > ref_stack.back()->max_size()))
661661
{
662662
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
663663
}
@@ -946,7 +946,7 @@ class json_sax_acceptor
946946
return true;
947947
}
948948

949-
bool start_object(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
949+
bool start_object(std::size_t /*unused*/ = std::numeric_limits<std::size_t>::max())
950950
{
951951
return true;
952952
}
@@ -961,7 +961,7 @@ class json_sax_acceptor
961961
return true;
962962
}
963963

964-
bool start_array(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
964+
bool start_array(std::size_t /*unused*/ = std::numeric_limits<std::size_t>::max())
965965
{
966966
return true;
967967
}

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(std::numeric_limits<std::size_t>::max())))
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(std::numeric_limits<std::size_t>::max())))
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 = std::numeric_limits<std::size_t>::max())
755755
{
756756
#if JSON_DIAGNOSTICS
757-
if (old_capacity != static_cast<std::size_t>(-1))
757+
if (old_capacity != std::numeric_limits<std::size_t>::max())
758758
{
759759
// see https://github.com/nlohmann/json/issues/2838
760760
JSON_ASSERT(type() == value_t::array);

single_include/nlohmann/json.hpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8774,7 +8774,7 @@ class json_sax_dom_parser
87748774
}
87758775
#endif
87768776

8777-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
8777+
if (JSON_HEDLEY_UNLIKELY(len != std::numeric_limits<std::size_t>::max() && len > ref_stack.back()->max_size()))
87788778
{
87798779
JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
87808780
}
@@ -8823,7 +8823,7 @@ class json_sax_dom_parser
88238823
}
88248824
#endif
88258825

8826-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
8826+
if (JSON_HEDLEY_UNLIKELY(len != std::numeric_limits<std::size_t>::max() && len > ref_stack.back()->max_size()))
88278827
{
88288828
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
88298829
}
@@ -9091,7 +9091,7 @@ class json_sax_dom_callback_parser
90919091
#endif
90929092

90939093
// check object limit
9094-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
9094+
if (JSON_HEDLEY_UNLIKELY(len != std::numeric_limits<std::size_t>::max() && len > ref_stack.back()->max_size()))
90959095
{
90969096
JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
90979097
}
@@ -9189,7 +9189,7 @@ class json_sax_dom_callback_parser
91899189
#endif
91909190

91919191
// check array limit
9192-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
9192+
if (JSON_HEDLEY_UNLIKELY(len != std::numeric_limits<std::size_t>::max() && len > ref_stack.back()->max_size()))
91939193
{
91949194
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
91959195
}
@@ -9478,7 +9478,7 @@ class json_sax_acceptor
94789478
return true;
94799479
}
94809480

9481-
bool start_object(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
9481+
bool start_object(std::size_t /*unused*/ = std::numeric_limits<std::size_t>::max())
94829482
{
94839483
return true;
94849484
}
@@ -9493,7 +9493,7 @@ class json_sax_acceptor
94939493
return true;
94949494
}
94959495

9496-
bool start_array(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
9496+
bool start_array(std::size_t /*unused*/ = std::numeric_limits<std::size_t>::max())
94979497
{
94989498
return true;
94999499
}
@@ -9825,7 +9825,7 @@ class binary_reader
98259825
std::int32_t document_size{};
98269826
get_number<std::int32_t, true>(input_format_t::bson, document_size);
98279827

9828-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
9828+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::numeric_limits<std::size_t>::max())))
98299829
{
98309830
return false;
98319831
}
@@ -10047,7 +10047,7 @@ class binary_reader
1004710047
std::int32_t document_size{};
1004810048
get_number<std::int32_t, true>(input_format_t::bson, document_size);
1004910049

10050-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
10050+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::numeric_limits<std::size_t>::max())))
1005110051
{
1005210052
return false;
1005310053
}
@@ -10307,7 +10307,7 @@ class binary_reader
1030710307
}
1030810308

1030910309
case 0x9F: // array (indefinite length)
10310-
return get_cbor_array(static_cast<std::size_t>(-1), tag_handler);
10310+
return get_cbor_array(std::numeric_limits<std::size_t>::max(), tag_handler);
1031110311

1031210312
// map (0x00..0x17 pairs of data items follow)
1031310313
case 0xA0:
@@ -10361,7 +10361,7 @@ class binary_reader
1036110361
}
1036210362

1036310363
case 0xBF: // map (indefinite length)
10364-
return get_cbor_object(static_cast<std::size_t>(-1), tag_handler);
10364+
return get_cbor_object(std::numeric_limits<std::size_t>::max(), tag_handler);
1036510365

1036610366
case 0xC6: // tagged item
1036710367
case 0xC7:
@@ -10749,7 +10749,7 @@ class binary_reader
1074910749
}
1075010750

1075110751
/*!
10752-
@param[in] len the length of the array or static_cast<std::size_t>(-1) for an
10752+
@param[in] len the length of the array or std::numeric_limits<std::size_t>::max() for an
1075310753
array of indefinite size
1075410754
@param[in] tag_handler how CBOR tags should be treated
1075510755
@return whether array creation completed
@@ -10762,7 +10762,7 @@ class binary_reader
1076210762
return false;
1076310763
}
1076410764

10765-
if (len != static_cast<std::size_t>(-1))
10765+
if (len != std::numeric_limits<std::size_t>::max())
1076610766
{
1076710767
for (std::size_t i = 0; i < len; ++i)
1076810768
{
@@ -10787,7 +10787,7 @@ class binary_reader
1078710787
}
1078810788

1078910789
/*!
10790-
@param[in] len the length of the object or static_cast<std::size_t>(-1) for an
10790+
@param[in] len the length of the object or std::numeric_limits<std::size_t>::max() for an
1079110791
object of indefinite size
1079210792
@param[in] tag_handler how CBOR tags should be treated
1079310793
@return whether object creation completed
@@ -10803,7 +10803,7 @@ class binary_reader
1080310803
if (len != 0)
1080410804
{
1080510805
string_t key;
10806-
if (len != static_cast<std::size_t>(-1))
10806+
if (len != std::numeric_limits<std::size_t>::max())
1080710807
{
1080810808
for (std::size_t i = 0; i < len; ++i)
1080910809
{
@@ -12221,7 +12221,7 @@ class binary_reader
1222112221
}
1222212222
else
1222312223
{
12224-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
12224+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::numeric_limits<std::size_t>::max())))
1222512225
{
1222612226
return false;
1222712227
}
@@ -12299,7 +12299,7 @@ class binary_reader
1229912299
}
1230012300
else
1230112301
{
12302-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
12302+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::numeric_limits<std::size_t>::max())))
1230312303
{
1230412304
return false;
1230512305
}
@@ -12635,7 +12635,7 @@ class binary_reader
1263512635
}
1263612636

1263712637
private:
12638-
static JSON_INLINE_VARIABLE constexpr std::size_t npos = static_cast<std::size_t>(-1);
12638+
static JSON_INLINE_VARIABLE constexpr std::size_t npos = std::numeric_limits<std::size_t>::max();
1263912639

1264012640
/// input adapter
1264112641
InputAdapterType ia;
@@ -12905,7 +12905,7 @@ class parser
1290512905
{
1290612906
case token_type::begin_object:
1290712907
{
12908-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
12908+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::numeric_limits<std::size_t>::max())))
1290912909
{
1291012910
return false;
1291112911
}
@@ -12950,7 +12950,7 @@ class parser
1295012950

1295112951
case token_type::begin_array:
1295212952
{
12953-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
12953+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::numeric_limits<std::size_t>::max())))
1295412954
{
1295512955
return false;
1295612956
}
@@ -20606,10 +20606,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
2060620606
return it;
2060720607
}
2060820608

20609-
reference set_parent(reference j, std::size_t old_capacity = static_cast<std::size_t>(-1))
20609+
reference set_parent(reference j, std::size_t old_capacity = std::numeric_limits<std::size_t>::max())
2061020610
{
2061120611
#if JSON_DIAGNOSTICS
20612-
if (old_capacity != static_cast<std::size_t>(-1))
20612+
if (old_capacity != std::numeric_limits<std::size_t>::max())
2061320613
{
2061420614
// see https://github.com/nlohmann/json/issues/2838
2061520615
JSON_ASSERT(type() == value_t::array);

0 commit comments

Comments
 (0)