Skip to content

Commit 85a99ca

Browse files
committed
Fix non-UTF8 crash for (date|time)_fromisoformat
Previously this would end up dereferencing a NULL pointer if the PyUnicode_AsUTF8AndSize call failed, this makes it so that the same error as any other parsing error is raised.
1 parent 2f48e53 commit 85a99ca

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

Modules/_datetimemodule.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,6 +2883,9 @@ date_fromisoformat(PyObject *cls, PyObject *dtstr) {
28832883
Py_ssize_t len;
28842884

28852885
const char * dt_ptr = PyUnicode_AsUTF8AndSize(dtstr, &len);
2886+
if (dt_ptr == NULL) {
2887+
goto invalid_string_error;
2888+
}
28862889

28872890
int year = 0, month = 0, day = 0;
28882891

@@ -2894,12 +2897,15 @@ date_fromisoformat(PyObject *cls, PyObject *dtstr) {
28942897
}
28952898

28962899
if (rv < 0) {
2897-
PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %s",
2898-
dt_ptr);
2899-
return NULL;
2900+
goto invalid_string_error;
29002901
}
29012902

29022903
return new_date_subclass_ex(year, month, day, cls);
2904+
2905+
invalid_string_error:
2906+
PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R",
2907+
dtstr);
2908+
return NULL;
29032909
}
29042910

29052911

@@ -4258,15 +4264,18 @@ time_fromisoformat(PyObject *cls, PyObject *tstr) {
42584264
Py_ssize_t len;
42594265
const char *p = PyUnicode_AsUTF8AndSize(tstr, &len);
42604266

4267+
if (p == NULL) {
4268+
goto invalid_string_error;
4269+
}
4270+
42614271
int hour = 0, minute = 0, second = 0, microsecond = 0;
42624272
int tzoffset, tzimicrosecond = 0;
42634273
int rv = parse_isoformat_time(p, len,
42644274
&hour, &minute, &second, &microsecond,
42654275
&tzoffset, &tzimicrosecond);
42664276

42674277
if (rv < 0) {
4268-
PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %s", p);
4269-
return NULL;
4278+
goto invalid_string_error;
42704279
}
42714280

42724281
PyObject *tzinfo = tzinfo_from_isoformat_results(rv, tzoffset,
@@ -4286,6 +4295,10 @@ time_fromisoformat(PyObject *cls, PyObject *tstr) {
42864295

42874296
Py_DECREF(tzinfo);
42884297
return t;
4298+
4299+
invalid_string_error:
4300+
PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R", tstr);
4301+
return NULL;
42894302
}
42904303

42914304

0 commit comments

Comments
 (0)