Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: Avoid UB in safe_[add,sub,mul] helpers (#31396)#31401

Merged
charris merged 1 commit into
numpy:maintenance/2.4.xfrom
charris:backport-31396
May 8, 2026
Merged

BUG: Avoid UB in safe_[add,sub,mul] helpers (#31396)#31401
charris merged 1 commit into
numpy:maintenance/2.4.xfrom
charris:backport-31396

Conversation

@charris
Copy link
Copy Markdown
Member

@charris charris commented May 8, 2026

Backport of #31396.

Over in https://github.com/numpy/numpy/pull/31378/changes#r3190759945, I noticed that these helpers can trigger UB, since the result is always computed even if overflow might happen. A sufficiently aggressive compiler might optimize functionality away if it detects the undefined behavior.

Clang and gcc both have intrinsics to handle this, which we use if available. I had to add new probes for the add and sub builtin intrinsics. For the fallback cases, I added early returns to avoid triggering overflow.

I used Claude Opus 4.7 to help with this.

@charris charris added this to the 2.4.5 release milestone May 8, 2026
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs labels May 8, 2026
@charris charris merged commit a42bd48 into numpy:maintenance/2.4.x May 8, 2026
73 checks passed
@charris charris deleted the backport-31396 branch May 8, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 08 - Backport Used to tag backport PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants