-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: json.decode fails for nums larger than sys.maxsize #34984
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
Conversation
@WillAyd I've made some headway on this. I've fixed
so that for intValue , the variable into which we read the incoming int, when it overflows it's reset rather than throwing a ValueError . If I understand this we'd want to store these pre-reset intValues in some way and then combine them into a PyLong object in Object_newBigNum .
Is that right, and if yes what's the best way to go forward? For example, would we want to use a
|
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.
Probably a few approaches here but first that comes to mind is to store the digits in a c string and call PyLong_FromString once all have been read
return SetError(ds, -1, overflowLimit == LLONG_MAX | ||
? "Value is too big" | ||
: "Value is too small"); | ||
if (intValue*10ULL + newDigit > overflowLimit) { |
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.
I don't think this works generally. If you want a reference for overflow detection try looking at the python implementation of PyLong_AsLongLongAndOverflow
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.
I feel like this might be ok because overflowLimit is adjusted earlier on depending on whether the number is positive or negative (in line 140 overflowLimit
gets changed if intValue came with a minus sign):
JSUINT64 overflowLimit = LLONG_MAX; |
But I'm down to rewrite this part to follow the PyLong_AsLongLongAndOverflow implementation:
https://github.com/python/cpython/blob/04cdeb7a5617c48102f45b965e683b12cdf934f8/Objects/longobject.c#L380
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.
The problem is if intValue*10ULL + newDigit
overflows than the comparison to overflowLimit
is a moot point because you've already hit undefined behavior with the former
@@ -64,6 +65,7 @@ struct DecoderState { | |||
int escHeap; | |||
int lastType; | |||
JSUINT32 objDepth; | |||
char *cStr; // storage for BigNum |
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.
No need to change the struct here; just look at the existing implementation of decode_string
and mirror that
newDigit = (JSLONG)(chr - 48); | ||
|
||
// TO DO: need to fix overflow catching | ||
if (intValue> (overflowLimit-newDigit)/10) { |
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.
@WillAyd Will this work? (intValue
is always positive so it's always floor division on the RHS)
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.
I don't think so. Assuming a 64 bit platform where the largest integer is 2 ** 63 - 1 this would not branch for 2 ** 63 (which it should)
Closing for now. I'll open a new request when I've got something ready for review |
I have an unfinished PR in the works, planning to finish it soon-ish (1.2 is probably a stretch, next release more likely) - unless you'd like to do a PR? |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
Follow-up to #34473