Skip to content

Conversation

@ngoldbaum
Copy link
Member

@ngoldbaum ngoldbaum commented Oct 24, 2025

c.f. https://huntr.com/bounties/49928a2c-c6bb-4c1c-80ec-5d7bf708bf28 where this almost led to a CVE getting reported against NumPy.

Addresses one of the issues reported in #28829.

For those who are unaware: the value of INT_MIN is -INT_MAX - 1 (negating using two's complement arithmetic), so that means that if C used two's complement -INT_MIN == INT_MIN. For that reason, -INT_MIN is UB according to the C standard. You are always supposed to do a check like this when negating a signed integer, but it is often skipped.

I learned while working on this that passing round a negative ndigits is supported. Round with ndigits!=0 is the same as round with ndigits==0, but transformed in the following way: round(x * 10**ndigits, 0) / 10**ndigits. The same formula holds for positive and negative ndigits.

No tests because it's annoying to write a test for this case (see python/cpython#132474 -- it turns out round(2**31, -2**31) will hang CPython...).

Open to suggestions if people want to do a more thorough job of this but at least this prevents the segfault.

Ping @devdanzin, this seems relevant to your interests.

@ngoldbaum ngoldbaum added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported labels Oct 24, 2025
@devdanzin
Copy link

Thanks for the ping and for the reference! I guess having a report avoid a CVE isn't as sexy as having a report result in a CVE, but it's still cool :)

Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

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

Thanks, could test the special case, but I am OK with just putting it in as is. (The result seems 0 or inf, although I guess the inf is just overflowing intermediates.)

Please go ahead and merge if you think a special case test isn't worth it, happy with that..

@charris charris merged commit 8afe118 into numpy:main Oct 25, 2025
79 checks passed
charris pushed a commit to charris/numpy that referenced this pull request Oct 25, 2025
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Oct 25, 2025
charris added a commit that referenced this pull request Oct 25, 2025
BUG: avoid negating INT_MIN in PyArray_Round implementation (#30071)
cakedev0 pushed a commit to cakedev0/numpy that referenced this pull request Dec 5, 2025
IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants