Skip to content

Commit 2422cd2

Browse files
codenutslowriot
authored andcommitted
Modernize integer comparison (nlohmann#4577)
Replace static_cast<size_t>(-1) with std::numeric_limits<std::size_t>::max() via the detail::unknown_size() function
1 parent 2214e05 commit 2422cd2

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
}
@@ -400,7 +400,7 @@ class binary_reader
400400
std::int32_t document_size{};
401401
get_number<std::int32_t, true>(input_format_t::bson, document_size);
402402

403-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
403+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
404404
{
405405
return false;
406406
}
@@ -660,7 +660,7 @@ class binary_reader
660660
}
661661

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

665665
// map (0x00..0x17 pairs of data items follow)
666666
case 0xA0:
@@ -714,7 +714,7 @@ class binary_reader
714714
}
715715

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

719719
case 0xC6: // tagged item
720720
case 0xC7:
@@ -1102,7 +1102,7 @@ class binary_reader
11021102
}
11031103

11041104
/*!
1105-
@param[in] len the length of the array or static_cast<std::size_t>(-1) for an
1105+
@param[in] len the length of the array or detail::unknown_size() for an
11061106
array of indefinite size
11071107
@param[in] tag_handler how CBOR tags should be treated
11081108
@return whether array creation completed
@@ -1115,7 +1115,7 @@ class binary_reader
11151115
return false;
11161116
}
11171117

1118-
if (len != static_cast<std::size_t>(-1))
1118+
if (len != detail::unknown_size())
11191119
{
11201120
for (std::size_t i = 0; i < len; ++i)
11211121
{
@@ -1140,7 +1140,7 @@ class binary_reader
11401140
}
11411141

11421142
/*!
1143-
@param[in] len the length of the object or static_cast<std::size_t>(-1) for an
1143+
@param[in] len the length of the object or detail::unknown_size() for an
11441144
object of indefinite size
11451145
@param[in] tag_handler how CBOR tags should be treated
11461146
@return whether object creation completed
@@ -1156,7 +1156,7 @@ class binary_reader
11561156
if (len != 0)
11571157
{
11581158
string_t key;
1159-
if (len != static_cast<std::size_t>(-1))
1159+
if (len != detail::unknown_size())
11601160
{
11611161
for (std::size_t i = 0; i < len; ++i)
11621162
{
@@ -2574,7 +2574,7 @@ class binary_reader
25742574
}
25752575
else
25762576
{
2577-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
2577+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
25782578
{
25792579
return false;
25802580
}
@@ -2652,7 +2652,7 @@ class binary_reader
26522652
}
26532653
else
26542654
{
2655-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
2655+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
26562656
{
26572657
return false;
26582658
}
@@ -2988,7 +2988,7 @@ class binary_reader
29882988
}
29892989

29902990
private:
2991-
static JSON_INLINE_VARIABLE constexpr std::size_t npos = static_cast<std::size_t>(-1);
2991+
static JSON_INLINE_VARIABLE constexpr std::size_t npos = detail::unknown_size();
29922992

29932993
/// input adapter
29942994
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
}
@@ -10053,7 +10058,7 @@ class binary_reader
1005310058
std::int32_t document_size{};
1005410059
get_number<std::int32_t, true>(input_format_t::bson, document_size);
1005510060

10056-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
10061+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
1005710062
{
1005810063
return false;
1005910064
}
@@ -10313,7 +10318,7 @@ class binary_reader
1031310318
}
1031410319

1031510320
case 0x9F: // array (indefinite length)
10316-
return get_cbor_array(static_cast<std::size_t>(-1), tag_handler);
10321+
return get_cbor_array(detail::unknown_size(), tag_handler);
1031710322

1031810323
// map (0x00..0x17 pairs of data items follow)
1031910324
case 0xA0:
@@ -10367,7 +10372,7 @@ class binary_reader
1036710372
}
1036810373

1036910374
case 0xBF: // map (indefinite length)
10370-
return get_cbor_object(static_cast<std::size_t>(-1), tag_handler);
10375+
return get_cbor_object(detail::unknown_size(), tag_handler);
1037110376

1037210377
case 0xC6: // tagged item
1037310378
case 0xC7:
@@ -10755,7 +10760,7 @@ class binary_reader
1075510760
}
1075610761

1075710762
/*!
10758-
@param[in] len the length of the array or static_cast<std::size_t>(-1) for an
10763+
@param[in] len the length of the array or detail::unknown_size() for an
1075910764
array of indefinite size
1076010765
@param[in] tag_handler how CBOR tags should be treated
1076110766
@return whether array creation completed
@@ -10768,7 +10773,7 @@ class binary_reader
1076810773
return false;
1076910774
}
1077010775

10771-
if (len != static_cast<std::size_t>(-1))
10776+
if (len != detail::unknown_size())
1077210777
{
1077310778
for (std::size_t i = 0; i < len; ++i)
1077410779
{
@@ -10793,7 +10798,7 @@ class binary_reader
1079310798
}
1079410799

1079510800
/*!
10796-
@param[in] len the length of the object or static_cast<std::size_t>(-1) for an
10801+
@param[in] len the length of the object or detail::unknown_size() for an
1079710802
object of indefinite size
1079810803
@param[in] tag_handler how CBOR tags should be treated
1079910804
@return whether object creation completed
@@ -10809,7 +10814,7 @@ class binary_reader
1080910814
if (len != 0)
1081010815
{
1081110816
string_t key;
10812-
if (len != static_cast<std::size_t>(-1))
10817+
if (len != detail::unknown_size())
1081310818
{
1081410819
for (std::size_t i = 0; i < len; ++i)
1081510820
{
@@ -12227,7 +12232,7 @@ class binary_reader
1222712232
}
1222812233
else
1222912234
{
12230-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
12235+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
1223112236
{
1223212237
return false;
1223312238
}
@@ -12305,7 +12310,7 @@ class binary_reader
1230512310
}
1230612311
else
1230712312
{
12308-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
12313+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
1230912314
{
1231012315
return false;
1231112316
}
@@ -12641,7 +12646,7 @@ class binary_reader
1264112646
}
1264212647

1264312648
private:
12644-
static JSON_INLINE_VARIABLE constexpr std::size_t npos = static_cast<std::size_t>(-1);
12649+
static JSON_INLINE_VARIABLE constexpr std::size_t npos = detail::unknown_size();
1264512650

1264612651
/// input adapter
1264712652
InputAdapterType ia;
@@ -12911,7 +12916,7 @@ class parser
1291112916
{
1291212917
case token_type::begin_object:
1291312918
{
12914-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
12919+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
1291512920
{
1291612921
return false;
1291712922
}
@@ -12956,7 +12961,7 @@ class parser
1295612961

1295712962
case token_type::begin_array:
1295812963
{
12959-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
12964+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
1296012965
{
1296112966
return false;
1296212967
}
@@ -20612,10 +20617,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
2061220617
return it;
2061320618
}
2061420619

20615-
reference set_parent(reference j, std::size_t old_capacity = static_cast<std::size_t>(-1))
20620+
reference set_parent(reference j, std::size_t old_capacity = detail::unknown_size())
2061620621
{
2061720622
#if JSON_DIAGNOSTICS
20618-
if (old_capacity != static_cast<std::size_t>(-1))
20623+
if (old_capacity != detail::unknown_size())
2061920624
{
2062020625
// see https://github.com/nlohmann/json/issues/2838
2062120626
JSON_ASSERT(type() == value_t::array);

0 commit comments

Comments
 (0)