boostorg / json

A C++11 library for parsing and serializing JSON to and from a DOM container in memory.
https://boost.org/libs/json
Boost Software License 1.0
424 stars 94 forks source link

Null-dereference warning in value_to #994

Open infn-ke opened 3 months ago

infn-ke commented 3 months ago

I'm using boost 1.83 and gcc13.2 and can see a gcc compiler warning about a potential null de-reference.

In function 'boost::json::result<T> boost::json::detail::value_to_impl(sequence_conversion_tag, boost::json::try_value_to_tag<T>, const boost::json::value&, const Ctx&) [with T = std::array<unsigned char, 128>; Ctx = std::tuple<allow_exceptions, no_context>]',
    inlined from 'typename boost::json::result_for<T, boost::json::value>::type boost::json::try_value_to(const value&, const Context&) [with T = std::array<unsigned char, 128>; Context = std::tuple<detail::allow_exceptions, detail::no_context>]' at /usr/include/boost/json/value_to.hpp:230:33:
/usr/include/boost/json/detail/value_to.hpp:315:16: warning: potential null pointer dereference [-Wnull-dereference]
  315 |         *ins++ = std::move(*elem_res);

Is the check elem_res.has_error() insufficient to guarantee that no null-pointer de-reference will happen?

grisumbras commented 3 months ago

I couldn't reproduce it. Can you please change this snippet (https://godbolt.org/z/4az5z7eva ), so that it hits that warning?

infn-ke commented 3 months ago

@grisumbras I can see issue in a larger code base. Will try to re-create in a smaller context.