Skip to content

PyLong_FromString(): fix Coverity CID 1424951 #4738

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

Merged
merged 1 commit into from
Dec 7, 2017
Merged

PyLong_FromString(): fix Coverity CID 1424951 #4738

merged 1 commit into from
Dec 7, 2017

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Dec 6, 2017

Explicitly cast digits (Py_ssize_t) to double to fix the following
false-alarm warning from Coverity:

"fsize_z = digits * log_base_BASE[base] + 1;"

CID 1424951: Incorrect expression (UNINTENDED_INTEGER_DIVISION)
Dividing integer expressions "9223372036854775783UL" and "4UL", and
then converting the integer quotient to type "double". Any remainder,
or fractional part of the quotient, is ignored.

Explicitly cast digits (Py_ssize_t) to double to fix the following
false-alarm warning from Coverity:

"fsize_z = digits * log_base_BASE[base] + 1;"

CID 1424951: Incorrect expression (UNINTENDED_INTEGER_DIVISION)
Dividing integer expressions "9223372036854775783UL" and "4UL", and
then converting the integer quotient to type "double". Any remainder,
or fractional part of the quotient, is ignored.
Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

Fix LGTM, but I'm not particularly happy about the use of double arithmetic here. Surely we can come up with an integer-only solution to detecting overflow?

But that's orthogonal to this PR.

@vstinner
Copy link
Member Author

vstinner commented Dec 7, 2017

Fix LGTM, but I'm not particularly happy about the use of double arithmetic here. Surely we can come up with an integer-only solution to detecting overflow?

Honestly, I was very surprised to see the usage of double in PyLong_FromString()!

But I don't feel able to implement logarithm in fixed precision arithmetic or something like that, to replace the current code. If we overestimate the size of the integer, we can trigger an error whereas it was technically possible to create the integer.

I recall vaguely that we use simple arithmetic on integers to estimate the size of an integer (estimate logarithm only with integers) in another Python function, but I don't recall where, sorry :-)

cc @serhiy-storchaka: I recall vaguely that you increased the precison of this estimation. Do you recall where it was?

@vstinner vstinner merged commit dd431b3 into python:master Dec 7, 2017
@vstinner vstinner deleted the long_fromstring_coverity branch December 7, 2017 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants