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

No proper validation for month in Calendar Module #126476

Closed
gugupy opened this issue Nov 6, 2024 · 4 comments · Fixed by #126484
Closed

No proper validation for month in Calendar Module #126476

gugupy opened this issue Nov 6, 2024 · 4 comments · Fixed by #126484
Labels
3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@gugupy
Copy link

gugupy commented Nov 6, 2024

Bug report

Bug description:

In the calendar module, IllegalMonthError is not handled properly. For months greater than 12, an IndexError is raised instead.

formatmonthname do not raise IndexError for negative integer because list support negative integer indexing.

>>> import calendar

>>> calendar.month(2024, -1)
Traceback (most recent call last):
  File "<python-input-12>", line 1, in <module>
    calendar.month(2024, -1)
    ~~~~~~~~~~~~~~^^^^^^^^^^
  File "/Users/gugu/Code/cpython/Lib/calendar.py", line 395, in formatmonth
    for week in self.monthdays2calendar(theyear, themonth):
                ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/Users/gugu/Code/cpython/Lib/calendar.py", line 284, in monthdays2calendar
    days = list(self.itermonthdays2(year, month))
  File "/Users/gugu/Code/cpython/Lib/calendar.py", line 240, in itermonthdays2
    for i, d in enumerate(self.itermonthdays(year, month), self.firstweekday):
                ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gugu/Code/cpython/Lib/calendar.py", line 228, in itermonthdays
    day1, ndays = monthrange(year, month)
                  ~~~~~~~~~~^^^^^^^^^^^^^
  File "/Users/gugu/Code/cpython/Lib/calendar.py", line 165, in monthrange
    raise IllegalMonthError(month)
calendar.IllegalMonthError: bad month number -1; must be 1-12

>>> calendar.month(2024, 13)
Traceback (most recent call last):
  File "<python-input-13>", line 1, in <module>
    calendar.month(2024, 13)
    ~~~~~~~~~~~~~~^^^^^^^^^^
  File "/Users/gugu/Code/cpython/Lib/calendar.py", line 390, in formatmonth
    s = self.formatmonthname(theyear, themonth, 7 * (w + 1) - 1)
  File "/Users/gugu/Code/cpython/Lib/calendar.py", line 373, in formatmonthname
    s = month_name[themonth]
        ~~~~~~~~~~^^^^^^^^^^
  File "/Users/gugu/Code/cpython/Lib/calendar.py", line 103, in __getitem__
    funcs = self._months[i]
            ~~~~~~~~~~~~^^^
IndexError: list index out of range

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

@gugupy gugupy added the type-bug An unexpected behavior, bug, or error label Nov 6, 2024
@tomasr8 tomasr8 added stdlib Python modules in the Lib dir 3.14 new features, bugs and security fixes labels Nov 6, 2024
@tomasr8
Copy link
Member

tomasr8 commented Nov 6, 2024

I think it'd make sense to raise IllegalMonthError in both cases. Would you like to send a PR?

@gugupy
Copy link
Author

gugupy commented Nov 6, 2024

@Zheaoli Also add the validation for arguments, right after get options. Because the same issue happening for cli.

    if options.month is not None and options.month not in range(1, 13):
        parser.error(IllegalMonthError(options.month).__str__())
        sys.exit(1)

@gugupy
Copy link
Author

gugupy commented Nov 6, 2024

I think it'd make sense to raise IllegalMonthError in both cases. Would you like to send a PR?

Yes, but @Zheaoli already did it.

@ZeroIntensity
Copy link
Member

@Zheaoli For future reference, it's a good idea to let an issue author send a PR if asked, especially for first time contributors.

ethanfurman added a commit that referenced this issue Nov 15, 2024
…e input month is not correct (GH-126484)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 15, 2024
…hen the input month is not correct (pythonGH-126484)

(cherry picked from commit 3be7498)

Co-authored-by: Nadeshiko Manju <me@manjusaka.me>
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 15, 2024
…hen the input month is not correct (pythonGH-126484)

(cherry picked from commit 3be7498)

Co-authored-by: Nadeshiko Manju <me@manjusaka.me>
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
ethanfurman added a commit that referenced this issue Nov 15, 2024
…when the input month is not correct (GH-126484) (GH-126878)

gh-126476: Raise IllegalMonthError for calendar.formatmonth() when the input month is not correct (GH-126484)
(cherry picked from commit 3be7498)

Co-authored-by: Nadeshiko Manju <me@manjusaka.me>
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
ethanfurman added a commit that referenced this issue Nov 15, 2024
…when the input month is not correct (GH-126484) (GH-126879)

gh-126476: Raise IllegalMonthError for calendar.formatmonth() when the input month is not correct (GH-126484)
(cherry picked from commit 3be7498)

Co-authored-by: Nadeshiko Manju <me@manjusaka.me>
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
3 participants