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

Failed assertion in itertoolsmodule.c: itertools_count_impl for count(sys.maxsize) #126595

Closed
devdanzin opened this issue Nov 8, 2024 · 7 comments
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@devdanzin
Copy link
Contributor

devdanzin commented Nov 8, 2024

Crash report

What happened?

Calling itertools.count(sys.maxsize) aborts with the following message:

python -c "from sys import maxsize; from itertools import count; count(maxsize)"
python: ./Modules/itertoolsmodule.c:3325: itertools_count_impl: Assertion `(cnt != PY_SSIZE_T_MAX && long_cnt == NULL && fast_mode) || (cnt == PY_SSIZE_T_MAX && long_cnt != NULL && !fast_mode)' failed.
Aborted (core dumped)

Found using fusil by @vstinner.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux, Windows

Output from running 'python -VV' on the command line:

Python 3.14.0a1+ (heads/main:4ea214ea982, Nov 6 2024, 00:52:15) [MSC v.1941 64 bit (AMD64)]

Linked PRs

@devdanzin devdanzin added the type-crash A hard crash of the interpreter, possibly with a core dump label Nov 8, 2024
@picnixz picnixz added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 8, 2024
@picnixz
Copy link
Contributor

picnixz commented Nov 8, 2024

cc @rhettinger

@picnixz picnixz self-assigned this Nov 9, 2024
@picnixz picnixz added 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Nov 9, 2024
@skirpichev
Copy link
Member

Note, that on non-debug builds there is no crash:

Python 3.13.0rc1+ (heads/3.13:bd29ce8509, Aug 29 2024, 15:30:15) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import maxsize
>>> from itertools import count
>>> c = count(maxsize)
>>> next(c)
9223372036854775807
>>> next(c)
9223372036854775808
>>> next(c)
9223372036854775809

So, I don't think this should be backported.

On another hand, we have an issue with repl. Continuing the above example:

>>> count(maxsize)
count(<NULL>)

This seems to be fixed by pr.

@picnixz
Copy link
Contributor

picnixz commented Nov 9, 2024

Note, that on non-debug builds there is no crash:

I think we should still backport fixes for non-debug builds because people might want to use debug builds for themselves.

@devdanzin
Copy link
Contributor Author

>>> count(maxsize)
count(<NULL>)

Nice find! If we take it further, another assertion fails:

Python 3.14.0a1+ experimental free-threading build (heads/main:6293d00, Nov  9 2024, 07:56:06) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import maxsize; from itertools import count
>>> c = count(maxsize - 2)
>>> for x in c:
...     print(c, x)
... 
count(9223372036854775806) 9223372036854775805
python: Objects/unicodeobject.c:3046: unicode_fromformat_arg: Assertion `obj' failed.
Aborted (core dumped)

@picnixz
Copy link
Contributor

picnixz commented Nov 9, 2024

Ah the PR does not fix this second assertion on unicode objects. I'll work a bit more. Thanks for finding it.

@picnixz
Copy link
Contributor

picnixz commented Nov 9, 2024

Update: I found the issue. When the counter reaches PY_SSIZE_T_MAX, it does not set its long_cnt field. In particular, if you call repr() just after, the field will still be NULL (it's set by next only). I'm preparing the fix.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 12, 2024
vstinner pushed a commit that referenced this issue 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 issue 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]>
@picnixz
Copy link
Contributor

picnixz commented Nov 12, 2024

Closing since completed and backported. Thank you all.

@picnixz picnixz closed this as completed Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

3 participants