Skip to content

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

Closed
wants to merge 19 commits into from

Conversation

arw2019
Copy link
Member

@arw2019 arw2019 commented Jun 25, 2020

Follow-up to #34473

@WillAyd WillAyd added the IO JSON read_json, to_json, json_normalize label Jun 25, 2020
@arw2019
Copy link
Member Author

arw2019 commented Jun 26, 2020

@WillAyd I've made some headway on this. I've fixed decode_numeric

FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric(struct DecoderState *ds) {

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 wide_char similarly to what's done for string?

JSOBJ Object_newString(void *prv, wchar_t *start, wchar_t *end) {

Copy link
Member

@WillAyd WillAyd left a 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) {
Copy link
Member

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

Copy link
Member Author

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

Copy link
Member

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
Copy link
Member

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) {
Copy link
Member Author

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)

Copy link
Member

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)

@arw2019
Copy link
Member Author

arw2019 commented Jul 12, 2020

Closing for now. I'll open a new request when I've got something ready for review

@arw2019 arw2019 closed this Jul 12, 2020
@kinghuang
Copy link

@arw2019 Did you ever create a new PR, by any chance? Thanks to your MR #34473, I'm able to write out large numbers to JSON. But, I can't read them back in.

@arw2019
Copy link
Member Author

arw2019 commented Nov 29, 2020

@arw2019 Did you ever create a new PR, by any chance? Thanks to your MR #34473, I'm able to write out large numbers to JSON. But, I can't read them back in.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO JSON read_json, to_json, json_normalize
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OverflowError: Python int too large to convert to C long
3 participants