beached / daw_json_link

Fast, convenient JSON serialization and parsing in C++
https://beached.github.io/daw_json_link/
Boost Software License 1.0
474 stars 31 forks source link

JsonRangeCheck::CheckForNarrowing crashes without raising exception #413

Closed panic-sell closed 7 months ago

panic-sell commented 10 months ago

I'm on MSVC 19.38, using daw_json_link 3.23.1 from vcpkg.

Here's a small program illustrating the behavior:

#include <cstdint>
#include <print>
#include <daw/json/daw_json_link.h>

struct Thing {
    uint8_t u;
};

namespace daw::json {
template <>
struct json_data_contract<Thing> {
    using type = json_member_list<
        json_number<"u", uint8_t, options::number_opt(options::JsonRangeCheck::CheckForNarrowing)>
    >;
};
}

int
main() {
    using namespace daw::json;

    Thing thing;
    try {
        thing = from_json<Thing>(R"({"u": 256})");  // <-- doesn't actually raise, immediately aborts
    } catch (daw::json::json_exception e) {
        std::println("{}: ", e.what(), e.parse_location());
        return 1;
    }
    std::println("u = {}", thing.u);
}
beached commented 10 months ago

Thanks for pointing this out. The issue is that it is throwing std::out_of_range there and the catch is looking for a json_exception with a NumberOutOfRange reason. I'll have to move the narrow cast function from the other library into JSON Link.

In the mean time before that patch gets out, catching std::exception const & or std::out_of_range const & should work around the issue.

beached commented 10 months ago

Also, the narrow_cast check was noexcept, oops and thanks again

beached commented 10 months ago

The fix is done, I should have it out in the next couple days in Release branch

beached commented 9 months ago

The temporary fix, it will throw a different exception type is in the latest daw-header-libraries that daw-json-link vcpkg pulls in. In the next daw-json-link release it will throw a json_exception https://github.com/microsoft/vcpkg/issues/35998#event-11384532666