Skip to content

Conversation

@vitaly-burovoy
Copy link
Contributor

During development I got strange results when some integer values are passed as query parameters.
I expected 0xfffffffe as int4 should raise an exception, but it saved as "-2". Moreover, 0xffffffffe (there is one "f" more) gave the same result.

It was understandable that it is a result of a wrong typecasting.
Debugging led to this PR. Almost all comments are in commit messages.

Firstly I tried to use PyLong_AsLongAndOverflow and PyLong_AsLongLongAndOverflow but could not reraise exceptions if they are not connected with overflowing. That is why "try..except" is used here.

To make the code be compatible between systems I use sizeof(val) > X to make a compiler optimize the rest of a condition.

Also I think it is wise to have the same exception among all ranges (previously either ValueError or OverflowError raised depending on how big passed value is).

Since '0' is in the positive half, negative range is slightly wider: abs(min_value) == (max_value + 1)
Since there is no error in a value representation and PyLong_AsLong(-Long) raises exactly that exception if the value exceed the destination range, it should be OverflowError instead of ValueError. Also make error message be similar to one from the original OverflowError raised by PyLong_AsLong.
@elprans elprans self-requested a review May 24, 2017 12:53
Copy link
Member

@elprans elprans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Vitaly!

@elprans elprans merged commit dfcf135 into MagicStack:master May 24, 2017
@vitaly-burovoy vitaly-burovoy deleted the fix_integer_boundaries branch May 25, 2017 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants