-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check we don't overflow when casting down integers during parsing #4353
base: develop
Are you sure you want to change the base?
Conversation
const auto val = *j.template get_ptr<ArithmeticTypeSource*>(); | ||
const auto min = std::numeric_limits<ArithmeticTypeTarget>::min(); | ||
const auto max = std::numeric_limits<ArithmeticTypeTarget>::max(); | ||
if (val < min && val > max) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This if
will never be true, unless you get some weird results from val and min/max having different types. You want ||
not &&
. You probably also want min
and max
to have the same type as val
.
You would also only need to do this test if ArithmeticTypeTarget
and ArithmeticTypeSource
are different types, and then only if Target
is smaller than Source
, or if they have different signed-ness.
It might also be better to do this as a pre-test that just throws on out of range, and leave the current lines alone for the actual assignments.
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. |
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @ArnaudBienner |
* Make the check optional if the target type is greater or equal to source type, or if source is floating point number * Ignore infinity values, as they are already handled
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:
|
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.