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-126595: fix a crash when calling itertools.count(sys.maxsize) #126617

Merged
merged 6 commits into from
Nov 12, 2024

Conversation

picnixz
Copy link
Contributor

@picnixz picnixz commented Nov 9, 2024

skirpichev
skirpichev previously approved these changes Nov 9, 2024
Copy link
Member

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

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

LGTM

But I doubt this should be backported.

@picnixz
Copy link
Contributor Author

picnixz commented Nov 9, 2024

But I doubt this should be backported.

Considering it's a bug that makes the interpreter crash, I think this should.

@skirpichev
Copy link
Member

Considering it's a bug that makes the interpreter crash

Only for debug builds.

@picnixz
Copy link
Contributor Author

picnixz commented Nov 9, 2024

Yes, but I still think it's better to backport it since it can be used by third-party libraries (let's discuss it on the issue instead).

@picnixz
Copy link
Contributor Author

picnixz commented Nov 9, 2024

Ok, maybe it's better to have a separate PR actually.

@picnixz picnixz marked this pull request as ready for review November 9, 2024 13:36
@picnixz
Copy link
Contributor Author

picnixz commented Nov 9, 2024

I'm reverting the __repr__ check and delegate it to #126620. The rationale is that there are some free-threaded update to do and I want to ease backports.

Copy link
Member

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

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

Yep, this seems correct.

Edit: One nitpick (a slightly smaller diff):

diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 1201fa0949..78fbdcdf77 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -3291,6 +3291,9 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
                 PyErr_Clear();
                 fast_mode = 0;
             }
+            else if (cnt == PY_SSIZE_T_MAX) {
+                fast_mode = 0;
+            }
         }
     } else {
         cnt = 0;

@picnixz
Copy link
Contributor Author

picnixz commented Nov 10, 2024

I think it will be more likely to have cnt equal to maxsize rather than having an error. Which is why I put that test first.

@skirpichev
Copy link
Member

I think it will be more likely to have cnt equal to maxsize rather than having an error.

Well, in terms of probability - it's more likely to have an error (this happens on countable subset of integers), rather having cnt equal to some fixed value ;)

@picnixz
Copy link
Contributor Author

picnixz commented Nov 10, 2024

Actually I was wrong. I thought that the error only happened in bad situations but the overflow exception happens when you put something > maxsize so your fix is better!

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

@rhettinger
Copy link
Contributor

+1 for backporting

@vstinner vstinner merged commit 6e3bb8a into python:main Nov 12, 2024
39 checks passed
@miss-islington-app
Copy link

Thanks @picnixz for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 12, 2024
@bedevere-app
Copy link

bedevere-app bot commented Nov 12, 2024

GH-126739 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Nov 12, 2024
@vstinner vstinner added needs backport to 3.12 bug and security fixes and removed needs backport to 3.12 bug and security fixes labels Nov 12, 2024
@miss-islington-app
Copy link

Thanks @picnixz for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@vstinner
Copy link
Member

I don't know what is going on with the 3.12 backport, it seems to be stuck.

@vstinner
Copy link
Member

Merged, thanks @picnixz for the fix and thanks @devdanzin for the bug report.

@picnixz picnixz deleted the fix/itertools-counter-checks-126595 branch November 12, 2024 13:17
@bedevere-app
Copy link

bedevere-app bot commented Nov 12, 2024

GH-126740 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Nov 12, 2024
vstinner pushed a commit that referenced this pull request Nov 12, 2024
…ze)` (GH-126617) (#126740)

gh-126595: fix a crash when calling `itertools.count(sys.maxsize)` (#126617)
vstinner pushed a commit that referenced this pull request Nov 12, 2024
…ze)` (GH-126617) (#126739)

gh-126595: fix a crash when calling `itertools.count(sys.maxsize)` (GH-126617)
(cherry picked from commit 6e3bb8a)

Co-authored-by: Bénédikt Tran <[email protected]>
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.

5 participants