nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.25k stars 6.57k forks source link

Check we don't overflow when casting down integers during parsing #4353

Open ArnaudBienner opened 2 months ago

ArnaudBienner commented 2 months ago

Hi,

I've faced the following issue when using this library: I stored some numbers in small integers (because I expect to use values in a small interval in practice) but the static_cast made during parsing lead to invalid values because of the overflow.

I believe it would be nice to have this runtime check to notice possible overflows at runtime.

nlohmann commented 2 months ago

I am a bit concerned that adding overflow exceptions (though correct) are changing the behavior of the library in a breaking (and maybe also surprising) way.

github-actions[bot] commented 2 months ago

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @ArnaudBienner Please read and follow the Contribution Guidelines.

coveralls commented 2 months ago

Coverage Status

coverage: 100.0%. remained the same when pulling da5800cdccebdd531a09399c89df13ea6e57de21 on ArnaudBienner:develop into 8c391e04fe4195d8be862c97f38cfe10e2a3472e on nlohmann:develop.

ArnaudBienner commented 2 months ago

Thank you all for your feedback :)

Turns out the check was (by chance) working in the sample case I wrote for unit testing :( I should have been more careful and more exhaustive in my testing, sorry about that. I've added a few more unit tests.

In addition to fixing the check, I also change the code to: