From e035c9aba6b8718407a71111d396cb660a778827 Mon Sep 17 00:00:00 2001 From: Thomas Braun Date: Fri, 30 Aug 2019 15:23:11 +0200 Subject: [PATCH] parser::sax_parse_internal: Avoid integer conversion warning Clang UBSAN complains here with /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_bvector.h:158:20: runtime error: unsigned integer overflow: 0 - 1 cannot b e represented in type 'unsigned int' #0 0x61f497 in std::_Bit_iterator_base::_M_bump_down() /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_bvector.h:158:2 0 #1 0x61b7eb in bool nlohmann::detail::parser, std::allocator >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer> >::sax_parse_internal, std::allocator >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer> > >(nlohmann::detail::json_sax_dom_parser, std::allocator >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_ serializer> >*) /home/firma/devel/json/include/nlohmann/detail/input/json_sax.hpp #2 0x611f33 in nlohmann::detail::parser, std: :allocator >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer> >::parse(bool, nlohmann::basic_json, std::allocator >, bool, long, unsigned long, double, std::allocator, nlohmann:: adl_serializer>&) /home/firma/devel/json/include/nlohmann/detail/input/parser.hpp:116:13 #3 0x5d0522 in nlohmann::basic_json, std::allocator >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer>::parse(nlohmann::detail::input_adapter&&, std::function, std::allocator >, bool, lon g, unsigned long, double, std::allocator, nlohmann::adl_serializer> >::parse_event_t, nlohmann::basic_json, std::allocator >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer>&)>, bool)/ home/firma/devel/json/include/nlohmann/json.hpp:6160:41 #4 0x5af340 in _DOCTEST_ANON_FUNC_84() /home/firma/devel/json/test/src/unit-msgpack.cpp:1321:19 #5 0x657d5e in doctest::Context::run() /home/firma/devel/json/test/thirdparty/doctest/doctest.h:5938:21 #6 0x65c700 in main /home/firma/devel/json/test/thirdparty/doctest/doctest.h:6016:71 #7 0x7f17952e32e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0) #8 0x453f29 in _start (/home/firma/devel/json/build/test/test-msgpack+0x453f29) so to make it happy we use the "official" way of denoting the maximum value of a size_t instead of the shortcut by casting. --- include/nlohmann/detail/input/parser.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index 8d4febcbfa..6501dadd81 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -4,6 +4,7 @@ #include // isfinite #include // uint8_t #include // function +#include // numeric_limits #include // string #include // move #include // vector @@ -185,7 +186,7 @@ class parser { case token_type::begin_object: { - if (JSON_HEDLEY_UNLIKELY(not sax->start_object(std::size_t(-1)))) + if (JSON_HEDLEY_UNLIKELY(not sax->start_object((std::numeric_limits::max)()))) { return false; }