Skip to content

gh-126595: fix a crash when calling itertools.count(sys.maxsize)#126617

Merged
vstinner merged 6 commits into
python:mainfrom
picnixz:fix/itertools-counter-checks-126595
Nov 12, 2024
Merged

gh-126595: fix a crash when calling itertools.count(sys.maxsize)#126617
vstinner merged 6 commits into
python:mainfrom
picnixz:fix/itertools-counter-checks-126595

Conversation

@picnixz

@picnixz picnixz commented Nov 9, 2024

Copy link
Copy Markdown
Member

skirpichev
skirpichev previously approved these changes Nov 9, 2024

@skirpichev skirpichev left a comment

Copy link
Copy Markdown
Member

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

picnixz commented Nov 9, 2024

Copy link
Copy Markdown
Member Author

But I doubt this should be backported.

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

@skirpichev

Copy link
Copy Markdown
Member

Considering it's a bug that makes the interpreter crash

Only for debug builds.

@picnixz

picnixz commented Nov 9, 2024

Copy link
Copy Markdown
Member Author

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

picnixz commented Nov 9, 2024

Copy link
Copy Markdown
Member Author

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

picnixz commented Nov 9, 2024

Copy link
Copy Markdown
Member Author

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.

@skirpichev skirpichev left a comment

Copy link
Copy Markdown
Member

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

picnixz commented Nov 10, 2024

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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

picnixz commented Nov 10, 2024

Copy link
Copy Markdown
Member Author

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!

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@rhettinger

Copy link
Copy Markdown
Contributor

+1 for backporting

@vstinner vstinner merged commit 6e3bb8a into python:main Nov 12, 2024
@miss-islington-app

Copy link
Copy Markdown

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
…e)` (pythonGH-126617)

(cherry picked from commit 6e3bb8a)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@bedevere-app

bedevere-app Bot commented Nov 12, 2024

Copy link
Copy Markdown

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 only security fixes and removed needs backport to 3.12 only security fixes labels Nov 12, 2024
@miss-islington-app

Copy link
Copy Markdown

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

@vstinner

Copy link
Copy Markdown
Member

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

@vstinner

Copy link
Copy Markdown
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

bedevere-app Bot commented Nov 12, 2024

Copy link
Copy Markdown

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 only 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 <10796600+picnixz@users.noreply.github.com>
picnixz added a commit to picnixz/cpython that referenced this pull request Dec 8, 2024
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
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