-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
gh-145376: Fix refleak and null pointer deref in unusual error path of datetime module #145476
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
Open
eendebakpt
wants to merge
3
commits into
python:main
Choose a base branch
from
eendebakpt:refleak_datetime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+22
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2026-03-03-19-42-51.gh-issue-145376.T7nY3q.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix refleak and null pointer deref in unusual error path of :mod:`datetime`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3822,9 +3822,26 @@ iso_calendar_date_new_impl(PyTypeObject *type, int year, int week, | |||||||||
| return NULL; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| PyTuple_SET_ITEM(self, 0, PyLong_FromLong(year)); | ||||||||||
| PyTuple_SET_ITEM(self, 1, PyLong_FromLong(week)); | ||||||||||
| PyTuple_SET_ITEM(self, 2, PyLong_FromLong(weekday)); | ||||||||||
| PyObject *year_object = PyLong_FromLong(year); | ||||||||||
| if (year_object == NULL) { | ||||||||||
| Py_DECREF(self); | ||||||||||
| return NULL; | ||||||||||
| } | ||||||||||
| PyTuple_SET_ITEM(self, 0, year_object); | ||||||||||
|
|
||||||||||
| PyObject *week_object = PyLong_FromLong(week); | ||||||||||
| if (week_object == NULL) { | ||||||||||
| Py_DECREF(self); | ||||||||||
| return NULL; | ||||||||||
| } | ||||||||||
| PyTuple_SET_ITEM(self, 1, week_object); | ||||||||||
|
|
||||||||||
| PyObject *weekday_object = PyLong_FromLong(weekday); | ||||||||||
| if (weekday_object == NULL) { | ||||||||||
| Py_DECREF(self); | ||||||||||
| return NULL; | ||||||||||
| } | ||||||||||
| PyTuple_SET_ITEM(self, 2, weekday_object); | ||||||||||
|
|
||||||||||
| return (PyObject *)self; | ||||||||||
| } | ||||||||||
|
|
@@ -6891,9 +6908,9 @@ datetime_datetime_astimezone_impl(PyDateTime_DateTime *self, | |||||||||
| goto naive; | ||||||||||
| } | ||||||||||
| else if (!PyDelta_Check(offset)) { | ||||||||||
| Py_DECREF(offset); | ||||||||||
| PyErr_Format(PyExc_TypeError, "utcoffset() returned %.200s," | ||||||||||
| " expected timedelta or None", Py_TYPE(offset)->tp_name); | ||||||||||
|
Comment on lines
6911
to
6912
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While we are here, you can use
Suggested change
|
||||||||||
| Py_DECREF(offset); | ||||||||||
| return NULL; | ||||||||||
| } | ||||||||||
| /* result = self - offset */ | ||||||||||
|
|
||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove it, users should not be impacted by this change. The error case is very unlikely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggested adding it since this PR covers several cases increasing the likelyhood of one of them being hit. If you think it is better to remove it, I don't mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually, we don't document such fix for an unlikely corner case.
PyLong_FromLong()can only fail if the system has no more free memory.datetime.datetime.astimezone()reads freed memory, but it only occurs if utcoffset() returns the wrong type. It seems like nobody reported the issue (with a reproducer), so it sounds unlikely to crash in practice (reading freed memory just works in the common case).