Visitar URL original
gh-91421: Use constant value check during runtime by stoeckmann · Pull Request #91422 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20220508170656/https://github.com/python/cpython/pull/91422
Skip to content
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

gh-91421: Use constant value check during runtime #91422

Merged
merged 1 commit into from Apr 13, 2022

Conversation

Copy link
Contributor

@stoeckmann stoeckmann commented Apr 10, 2022

The left-hand side expression of the if-check can be converted to a
constant by the compiler, but the addition on the right-hand side is
performed during runtime.

Move the addition from the right-hand side to the left-hand side by
turning it into a subtraction there. Since the values are known to
be large enough to not turn negative, this is a safe operation.

Prevents a very unlikely integer overflow on 32 bit systems.

Fixes issue #91421.

@cpython-cla-bot
Copy link

@cpython-cla-bot cpython-cla-bot bot commented Apr 10, 2022 •

All commit authors signed the Contributor License Agreement.
CLA signed

@stoeckmann stoeckmann changed the title Use constant value check during runtime gh-91421: Use constant value check during runtime Apr 10, 2022
Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Thanks, this looks right to me but I'll leave others some time to speak up if there's something I missed.

This will need a NEWS entry, which you can add using blurb: https://devguide.python.org/?highlight=blurb#quick-reference. We're still working on updating some bpo references there, but I believe the blurb-it online tool has already been updated to point to GitHub.

@stoeckmann
Copy link
Author

@stoeckmann stoeckmann commented Apr 10, 2022

The blurb-it web tool required a bpo, but the command line tool blurb was already adjusted.

Copy link
Contributor

@CharlieZhao95 CharlieZhao95 left a comment

This looks good. One assembly instruction can be reduced.

@@ -0,0 +1 @@
Use constant value check during runtime in _Py_DecodeUTF8Ex.
Copy link
Member

@JelleZijlstra JelleZijlstra Apr 12, 2022

Choose a reason for hiding this comment

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

Suggested change
Use constant value check during runtime in _Py_DecodeUTF8Ex.
Fix a potential integer overflow in _Py_DecodeUTF8Ex.

Copy link
Contributor Author

@stoeckmann stoeckmann Apr 12, 2022

Choose a reason for hiding this comment

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

Adjusted.

The left-hand side expression of the if-check can be converted to a
constant by the compiler, but the addition on the right-hand side is
performed during runtime.

Move the addition from the right-hand side to the left-hand side by
turning it into a subtraction there. Since the values are known to
be large enough to not turn negative, this is a safe operation.

Prevents a very unlikely integer overflow on 32 bit systems.
@JelleZijlstra JelleZijlstra merged commit 0859368 into python:main Apr 13, 2022
13 checks passed
@miss-islington
Copy link

@miss-islington miss-islington commented Apr 13, 2022

Thanks @stoeckmann for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10.
🐍🍒⛏🤖

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Apr 13, 2022

GH-91492 is a backport of this pull request to the 3.10 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Apr 13, 2022
The left-hand side expression of the if-check can be converted to a
constant by the compiler, but the addition on the right-hand side is
performed during runtime.

Move the addition from the right-hand side to the left-hand side by
turning it into a subtraction there. Since the values are known to
be large enough to not turn negative, this is a safe operation.

Prevents a very unlikely integer overflow on 32 bit systems.

Fixes pythonGH-91421.
(cherry picked from commit 0859368)

Co-authored-by: Tobias Stoeckmann <stoeckmann@users.noreply.github.com>
@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Apr 13, 2022

GH-91493 is a backport of this pull request to the 3.9 branch.

gvanrossum pushed a commit to gvanrossum/cpython that referenced this issue Apr 13, 2022
The left-hand side expression of the if-check can be converted to a
constant by the compiler, but the addition on the right-hand side is
performed during runtime.

Move the addition from the right-hand side to the left-hand side by
turning it into a subtraction there. Since the values are known to
be large enough to not turn negative, this is a safe operation.

Prevents a very unlikely integer overflow on 32 bit systems.

Fixes pythonGH-91421.
JelleZijlstra pushed a commit that referenced this issue Apr 14, 2022
The left-hand side expression of the if-check can be converted to a
constant by the compiler, but the addition on the right-hand side is
performed during runtime.

Move the addition from the right-hand side to the left-hand side by
turning it into a subtraction there. Since the values are known to
be large enough to not turn negative, this is a safe operation.

Prevents a very unlikely integer overflow on 32 bit systems.

Fixes GH-91421.
(cherry picked from commit 0859368)

Co-authored-by: Tobias Stoeckmann <stoeckmann@users.noreply.github.com>
JelleZijlstra pushed a commit that referenced this issue Apr 14, 2022
The left-hand side expression of the if-check can be converted to a
constant by the compiler, but the addition on the right-hand side is
performed during runtime.

Move the addition from the right-hand side to the left-hand side by
turning it into a subtraction there. Since the values are known to
be large enough to not turn negative, this is a safe operation.

Prevents a very unlikely integer overflow on 32 bit systems.

Fixes GH-91421.
(cherry picked from commit 0859368)

Co-authored-by: Tobias Stoeckmann <stoeckmann@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants