Skip to content

bpo-31339: time.asctime() raise on year > 9999 #3296

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,8 @@ def test_asctime(self):
time.asctime(time.gmtime(self.t))
self.assertRaises(TypeError, time.asctime, 0)
self.assertRaises(TypeError, time.asctime, ())
# XXX: Posix compiant asctime should refuse to convert
# year > 9999, but Linux implementation does not.
# self.assertRaises(ValueError, time.asctime,
# (12345, 1, 0, 0, 0, 0, 0, 0, 0))
# XXX: For now, just make sure we don't have a crash:
try:
with self.assertRaises(ValueError):
time.asctime((12345, 1, 1, 0, 0, 0, 0, 1, 0))
except ValueError:
pass

@unittest.skipIf(not hasattr(time, "tzset"),
"time module has no attribute tzset")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
time.asctime() now raises a ValueError for year larger than 9999 to prevent a
crash when Python is run using the musl C library.
6 changes: 6 additions & 0 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,12 @@ time_asctime(PyObject *self, PyObject *args)
buf = *localtime(&tt);
} else if (!gettmarg(tup, &buf))
return NULL;
/* bpo-31339: Restrict to years < 10,000 so the output fits
into 24 characters */
if (buf.tm_year > 9999) {
PyErr_SetString(PyExc_ValueError, "year larger than 9999");
return NULL;
}
p = asctime(&buf);
if (p == NULL) {
PyErr_SetString(PyExc_ValueError, "invalid time");
Expand Down