Skip to content

./Modules/_datetimemodule.c:290: days_before_year: Assertion `year >= 1' failed. #117534

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

Closed
Vlad4896 opened this issue Apr 4, 2024 · 12 comments
Closed
Labels
extension-modules C modules in the Modules dir type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@Vlad4896
Copy link
Contributor

Vlad4896 commented Apr 4, 2024

Crash report

What happened?

Python terminates with core dumped on the following test:

import datetime

y = datetime.datetime.fromisoformat('0000W25')
print(y)

The test result:

python: ./Modules/_datetimemodule.c:290: days_before_year: Assertion `year >= 1' failed.
Aborted (core dumped)

The issue happened due to lack of checking for input parameters in iso_to_ymd().
The 'iso_year' input parameter should be checked to fix the issue.

CPython versions tested on:

3.11, 3.12, CPython main branch

Operating systems tested on:

Linux

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

Python 3.13.0a5+ (heads/main:dc54714044, Apr 4 2024, 12:28:42) [GCC 11.4.0]

Linked PRs

@Vlad4896 Vlad4896 added the type-crash A hard crash of the interpreter, possibly with a core dump label Apr 4, 2024
@Eclips4
Copy link
Member

Eclips4 commented Apr 4, 2024

Hello!
Thanks for the report.
Would you like to submit a PR with fix? :)

@Eclips4 Eclips4 added the extension-modules C modules in the Modules dir label Apr 4, 2024
@hugovk
Copy link
Member

hugovk commented Apr 4, 2024

For comparison, on macOS:

❯ ./python.exe
Python 3.13.0a5+ (heads/main:9dae05ee59, Apr  4 2024, 13:17:17) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> y = datetime.datetime.fromisoformat('0000W25')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    y = datetime.datetime.fromisoformat('0000W25')
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
ValueError: month must be in 1..12

Same with 3.11.8 and 3.12.2.

@erlend-aasland
Copy link
Contributor

I get the same results as Hugo on Ubuntu 24.04, Python 3.12.

@hugovk
Copy link
Member

hugovk commented Apr 4, 2024

Also on 3.11-3.13 on Windows, macOS and Ubuntu on GitHub Actions:

(3.8-3.10 also has a ValueError with a different error message.)

@Vlad4896
Copy link
Contributor Author

Vlad4896 commented Apr 4, 2024

It is interesting. I've just forked the repository, make and run my test.
I've used the following command to build Python:
./configure --with-pydebug && make -j8

After it I got the Assertion:
python: ./Modules/_datetimemodule.c:290: days_before_year: Assertion `year >= 1' failed.
Aborted (core dumped)

@erlend-aasland
Copy link
Contributor

Could you try this, just to be sure:

$ git clean -fdx
$ ./configure --with-pydebug
$ make  # no -j8

@Eclips4
Copy link
Member

Eclips4 commented Apr 4, 2024

Debug build on current main branch (clean build), macOS Sonoma 14.2.1:

./python.exe example.py                                
Assertion failed: (year >= 1), function days_before_year, file _datetimemodule.c, line 290.
zsh: abort      ./python.exe example.py

@Vlad4896
Copy link
Contributor Author

Vlad4896 commented Apr 4, 2024

I've tried as suggested Erlend and got the same result:
$ ./python mytest.py
python: ./Modules/_datetimemodule.c:290: days_before_year: Assertion `year >= 1' failed.
Aborted (core dumped)

I've the following back trace for this:

#1  __pthread_kill_internal (signo=6, threadid=140737352808256) at ./nptl/pthread_kill.c:78
#2  __GI___pthread_kill (threadid=140737352808256, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
#3  0x00007ffff7c42476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4  0x00007ffff7c287f3 in __GI_abort () at ./stdlib/abort.c:79
#5  0x00007ffff7c2871b in __assert_fail_base (fmt=0x7ffff7ddd150 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=0x7ffff7e4e0e0 "year >= 1", 
    file=0x7ffff7e4e000 "./Modules/_datetimemodule.c", line=290, function=<optimized out>) at ./assert/assert.c:92
#6  0x00007ffff7c39e96 in __GI___assert_fail (assertion=assertion@entry=0x7ffff7e4e0e0 "year >= 1", file=file@entry=0x7ffff7e4e000 "./Modules/_datetimemodule.c", line=line@entry=290, 
    function=function@entry=0x7ffff7e50520 <__PRETTY_FUNCTION__.0> "days_before_year") at ./assert/assert.c:101
#7  0x00007ffff7e3d6bf in days_before_year (year=year@entry=0) at ./Modules/_datetimemodule.c:290
#8  0x00007ffff7e3d6d8 in ymd_to_ord (year=year@entry=0, month=month@entry=1, day=day@entry=1) at ./Modules/_datetimemodule.c:389
#9  0x00007ffff7e3d73b in iso_week1_monday (year=year@entry=0) at ./Modules/_datetimemodule.c:405
#10 0x00007ffff7e3d7d6 in iso_to_ymd (iso_year=0, iso_week=25, iso_day=1, year=year@entry=0x7fffffffd71c, month=month@entry=0x7fffffffd720, day=day@entry=0x7fffffffd724)
    at ./Modules/_datetimemodule.c:440
#11 0x00007ffff7e3d998 in parse_isoformat_date (dtstr=dtstr@entry=0x7ffff74dd328 "0000W25", len=len@entry=7, year=year@entry=0x7fffffffd71c, month=month@entry=0x7fffffffd720, 
    day=day@entry=0x7fffffffd724) at ./Modules/_datetimemodule.c:803
#12 0x00007ffff7e479c0 in datetime_fromisoformat (cls=0x7ffff7e58000 <PyDateTime_DateTimeType>, dtstr=0x7ffff74dd300) at ./Modules/_datetimemodule.c:5506
#13 0x00005555556f8c0b in cfunction_vectorcall_O (func=0x7ffff74d0ef0, args=<optimized out>, nargsf=<optimized out>, kwnames=<optimized out>) at Objects/methodobject.c:512
#14 0x000055555568692c in _PyObject_VectorcallTstate (tstate=0x555555c04510 <_PyRuntime+280656>, callable=0x7ffff74d0ef0, args=0x7ffff7fb0078, nargsf=9223372036854775809, kwnames=0x0)
    at ./Include/internal/pycore_call.h:168
#15 0x0000555555686a4b in PyObject_Vectorcall (callable=callable@entry=0x7ffff74d0ef0, args=args@entry=0x7ffff7fb0078, nargsf=<optimized out>, kwnames=kwnames@entry=0x0) at Objects/call.c:327
#16 0x00005555558124c1 in _PyEval_EvalFrameDefault (tstate=tstate@entry=0x555555c04510 <_PyRuntime+280656>, frame=0x7ffff7fb0020, throwflag=throwflag@entry=0) at Python/generated_cases.c.h:813
#17 0x0000555555834f71 in _PyEval_EvalFrame (throwflag=0, frame=<optimized out>, tstate=0x555555c04510 <_PyRuntime+280656>) at ./Include/internal/pycore_ceval.h:114
#18 _PyEval_Vector (tstate=tstate@entry=0x555555c04510 <_PyRuntime+280656>, func=func@entry=0x7ffff746a2d0, locals=locals@entry=0x7ffff74785f0, args=args@entry=0x0, argcount=argcount@entry=0, 
    kwnames=kwnames@entry=0x0) at Python/ceval.c:1807
#19 0x000055555583502f in PyEval_EvalCode (co=co@entry=0x7ffff75a2480, globals=globals@entry=0x7ffff74785f0, locals=locals@entry=0x7ffff74785f0) at Python/ceval.c:600
#20 0x00005555558bc6ae in run_eval_code_obj (tstate=tstate@entry=0x555555c04510 <_PyRuntime+280656>, co=co@entry=0x7ffff75a2480, globals=globals@entry=0x7ffff74785f0, 
    locals=locals@entry=0x7ffff74785f0) at Python/pythonrun.c:1291
#21 0x00005555558bdf7b in run_mod (mod=mod@entry=0x555555d32e28, filename=filename@entry=0x7ffff74a5ac0, globals=globals@entry=0x7ffff74785f0, locals=locals@entry=0x7ffff74785f0, 
    flags=flags@entry=0x7fffffffdc68, arena=arena@entry=0x7ffff74d0ca0, interactive_src=0x0, generate_new_source=0) at Python/pythonrun.c:1376
#22 0x00005555558be660 in pyrun_file (fp=fp@entry=0x555555cac010, filename=filename@entry=0x7ffff74a5ac0, start=start@entry=257, globals=globals@entry=0x7ffff74785f0, 
    locals=locals@entry=0x7ffff74785f0, closeit=closeit@entry=1, flags=0x7fffffffdc68) at Python/pythonrun.c:1212
#23 0x00005555558bf87d in _PyRun_SimpleFileObject (fp=fp@entry=0x555555cac010, filename=filename@entry=0x7ffff74a5ac0, closeit=closeit@entry=1, flags=flags@entry=0x7fffffffdc68)
    at Python/pythonrun.c:461
#24 0x00005555558bfb50 in _PyRun_AnyFileObject (fp=fp@entry=0x555555cac010, filename=filename@entry=0x7ffff74a5ac0, closeit=closeit@entry=1, flags=flags@entry=0x7fffffffdc68)
    at Python/pythonrun.c:77
#25 0x00005555558ea520 in pymain_run_file_obj (program_name=program_name@entry=0x7ffff74a5b40, filename=filename@entry=0x7ffff74a5ac0, skip_source_first_line=0) at Modules/main.c:357
#26 0x00005555558eb76b in pymain_run_file (config=config@entry=0x555555bd7438 <_PyRuntime+96120>) at Modules/main.c:376
#27 0x00005555558ebc45 in pymain_run_python (exitcode=exitcode@entry=0x7fffffffddf4) at Modules/main.c:628
--Type <RET> for more, q to quit, c to continue without paging-- 
#28 0x00005555558ebcb5 in Py_RunMain () at Modules/main.c:707
#29 0x00005555558ebd2c in pymain_main (args=args@entry=0x7fffffffde50) at Modules/main.c:737
#30 0x00005555558ebdef in Py_BytesMain (argc=<optimized out>, argv=<optimized out>) at Modules/main.c:761
#31 0x00005555555d48c6 in main (argc=<optimized out>, argv=<optimized out>) at ./Programs/python.c:15

@erlend-aasland
Copy link
Contributor

I can reproduce on a fresh build on macOS. HEAD at b32789c.

@terryjreedy
Copy link
Member

I got ValueError on Windows installed 3.13.0a5, March 12. Crash on fresh debug no-gil build, so there is a regression for me also.

Vlad4896 added a commit to Vlad4896/cpython that referenced this issue Apr 5, 2024
Vlad4896 added a commit to Vlad4896/cpython that referenced this issue Apr 5, 2024
pganssle pushed a commit that referenced this issue Apr 9, 2024
Moves the validation for invalid years in the C implementation of the `datetime` module into a common location between `fromisoformat` and `fromisocalendar`, which improves the error message and fixes a failed assertion when parsing invalid ISO 8601 years using one of the "ISO weeks" formats.

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Apr 9, 2024
…onGH-117543)

Moves the validation for invalid years in the C implementation of the `datetime` module into a common location between `fromisoformat` and `fromisocalendar`, which improves the error message and fixes a failed assertion when parsing invalid ISO 8601 years using one of the "ISO weeks" formats.

---------

(cherry picked from commit d5f1139)

Co-authored-by: Vlad4896 <[email protected]>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
@pganssle
Copy link
Member

pganssle commented Apr 9, 2024

I was able to reproduce this with ./configure --with-assertions. In some ways the core dump is an overactive assertion error, because this particular case incidentally fails for other reasons (hence the weird message about months being out of range), but regardless of whether it's an interpreter crash, it still should be fixed.

Should be fixed with #117534.

@erlend-aasland
Copy link
Contributor

Should be fixed with #117534.

That would be #117543 :)

pganssle pushed a commit that referenced this issue Apr 9, 2024
…117543) (#117689)

gh-117534: Add checking for input parameter in iso_to_ymd (GH-117543)

Moves the validation for invalid years in the C implementation of the `datetime` module into a common location between `fromisoformat` and `fromisocalendar`, which improves the error message and fixes a failed assertion when parsing invalid ISO 8601 years using one of the "ISO weeks" formats.

---------

(cherry picked from commit d5f1139)

Co-authored-by: Vlad4896 <[email protected]>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
…on#117543)

Moves the validation for invalid years in the C implementation of the `datetime` module into a common location between `fromisoformat` and `fromisocalendar`, which improves the error message and fixes a failed assertion when parsing invalid ISO 8601 years using one of the "ISO weeks" formats.

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-crash A hard crash of the interpreter, possibly with a core dump
Projects
Archived in project
Development

No branches or pull requests

6 participants