getml / reflect-cpp

A C++20 library for fast serialization, deserialization and validation using reflection. Supports JSON, BSON, CBOR, flexbuffers, msgpack, TOML, XML, YAML / msgpack.org[C++20]
https://getml.github.io/reflect-cpp/
MIT License
821 stars 65 forks source link

Parsing unordered map with a int64 key throws exception #85

Closed Lazrius closed 2 months ago

Lazrius commented 3 months ago

While trying to parse an unordered map (and I assume the behavior would be the same with a regular map), we were getting an exception.

Error when loading a character: Failed to parse field 'visits': Found 27 errors:
1) Failed to parse field '2636585985': stoi argument out of range
2) Failed to parse field '2660778562': stoi argument out of range
...

After looking around the code, I found the call that is erroring out: https://github.com/getml/reflect-cpp/blob/b5338f014203fb161756adf91687b35b936a18b6/include/rfl/parsing/MapReader.hpp#L45

The is integral call will succeed on 64bit int, regardless of whether they are signed or not, but then pass it into a function that expects a 32 bit value.

I would assume the best solution here is to have an extra if that checks if the size is 8 bytes and calls std::stoull instead.

liuzicheng1987 commented 3 months ago

Yes, I think that is the solution here.