Skip to content

[5.5] ConstantFolding: remove a wrong peephole optimization for signed "< 0" and ">= 0" comparisons #39946

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
Oct 29, 2021

Conversation

eeckstein
Copy link
Contributor

The wrong optimization was: fold x < 0 into false, if x is known to be a result of an unsigned operation with overflow checks enabled.
It was done under the wrong assumption that the result of an overflow-checked unsigned operation fits into a signed integer and is positive.
This is wrong, because the result of an unsigned operation can be larger than Int.max and therefore, when used in a signed integer operation, be re-interpreted as a negative signed value.

Fixes a miscompile which resulted in a missing abort on arithmetic overflow.

This is a cherry-pick of #39938

rdar://73596890

…" and ">= 0" comparisons

The wrong optimization was: fold x < 0 into false, if x is known to be a result of an unsigned operation with overflow checks enabled.
It was done under the wrong assumption that the result of an overflow-checked unsigned operation fits into a signed integer and is positive.
This is wrong, because the result of an unsigned operation can be larger than Int.max and therefore, when used in a signed integer operation, be re-interpreted as a negative signed value.

Fixes a miscompile which resulted in a missing abort on arithmetic overflow.

rdar://73596890
@eeckstein eeckstein added the r5.5 label Oct 28, 2021
@eeckstein eeckstein requested a review from a team as a code owner October 28, 2021 07:16
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein merged commit 76f61e6 into swiftlang:release/5.5 Oct 29, 2021
@eeckstein eeckstein deleted the fix-constant-folding-5.5 branch October 29, 2021 16:15
@AnthonyLatsis AnthonyLatsis added 🍒 release cherry pick Flag: Release branch cherry picks swift 5.5 labels Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍒 release cherry pick Flag: Release branch cherry picks swift 5.5
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants