Skip to content

Commit 166b6fe

Browse files
pythongh-124531: Fix strftime() with embedded null characters
* time.strftime() (raised ValueError) * the strftime() method and formatting of the datetime classes datetime, date and time (truncated at the null character)
1 parent 5e9e506 commit 166b6fe

5 files changed

Lines changed: 85 additions & 52 deletions

File tree

‎Lib/test/datetimetester.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,6 +2955,16 @@ def test_more_strftime(self):
29552955
except UnicodeEncodeError:
29562956
pass
29572957

2958+
def test_strftime_embedded_nul(self):
2959+
# gh-124531: The null character should not terminate the format string.
2960+
t = self.theclass(2004, 12, 31, 6, 22, 33, 47)
2961+
self.assertEqual(t.strftime('\0'), '\0')
2962+
self.assertEqual(t.strftime('\0'*1000), '\0'*1000)
2963+
s1 = t.strftime('%c')
2964+
s2 = t.strftime('%x')
2965+
self.assertEqual(t.strftime('\0%c\0%x'), f'\0{s1}\0{s2}')
2966+
self.assertEqual(t.strftime('\0%c\0%x\0'), f'\0{s1}\0{s2}\0')
2967+
29582968
def test_extract(self):
29592969
dt = self.theclass(2002, 3, 4, 18, 45, 3, 1234)
29602970
self.assertEqual(dt.date(), date(2002, 3, 4))
@@ -3736,6 +3746,16 @@ def test_strftime(self):
37363746
# gh-85432: The parameter was named "fmt" in the pure-Python impl.
37373747
t.strftime(format="%f")
37383748

3749+
def test_strftime_embedded_nul(self):
3750+
# gh-124531: The null character should not terminate the format string.
3751+
t = self.theclass(1, 2, 3, 4)
3752+
self.assertEqual(t.strftime('\0'), '\0')
3753+
self.assertEqual(t.strftime('\0'*1000), '\0'*1000)
3754+
s1 = t.strftime('%Z')
3755+
s2 = t.strftime('%X')
3756+
self.assertEqual(t.strftime('\0%Z\0%X'), f'\0{s1}\0{s2}')
3757+
self.assertEqual(t.strftime('\0%Z\0%X\0'), f'\0{s1}\0{s2}\0')
3758+
37393759
def test_format(self):
37403760
t = self.theclass(1, 2, 3, 4)
37413761
self.assertEqual(t.__format__(''), str(t))

‎Lib/test/test_time.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,16 @@ def test_strftime(self):
182182
self.fail('conversion specifier: %r failed.' % format)
183183

184184
self.assertRaises(TypeError, time.strftime, b'%S', tt)
185-
# embedded null character
186-
self.assertRaises(ValueError, time.strftime, '%S\0', tt)
185+
186+
def test_strftime_embedded_nul(self):
187+
# gh-124531: The null character should not terminate the format string.
188+
tt = time.gmtime(self.t)
189+
self.assertEqual(time.strftime('\0', tt), '\0')
190+
self.assertEqual(time.strftime('\0'*1000, tt), '\0'*1000)
191+
s1 = time.strftime('%c', tt)
192+
s2 = time.strftime('%x', tt)
193+
self.assertEqual(time.strftime('\0%c\0%x', tt), f'\0{s1}\0{s2}')
194+
self.assertEqual(time.strftime('\0%c\0%x\0', tt), f'\0{s1}\0{s2}\0')
187195

188196
def _bounds_checking(self, func):
189197
# Make sure that strftime() checks the bounds of the various parts
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix :func:`time.strftime`, the :meth:`~datetime.datetime.strftime` method of
2+
the :mod:`datetime` classes :class:`~datetime.datetime`,
3+
:class:`~datetime.date` and :class:`~datetime.time` and formatting of these
4+
classes with format strings containing embedded null characters.

‎Modules/_datetimemodule.c‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,7 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
18371837
PyObject *freplacement = NULL; /* py string, replacement for %f */
18381838

18391839
const char *pin; /* pointer to next char in input format */
1840+
const char *pend; /* pointer past the end of input format */
18401841
Py_ssize_t flen; /* length of input format */
18411842
char ch; /* next char in input format */
18421843

@@ -1886,22 +1887,15 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
18861887
pnew = PyBytes_AsString(newfmt);
18871888
usednew = 0;
18881889

1889-
while ((ch = *pin++) != '\0') {
1890-
if (ch != '%') {
1891-
ptoappend = pin - 1;
1892-
ntoappend = 1;
1893-
}
1894-
else if ((ch = *pin++) == '\0') {
1895-
/* Null byte follows %, copy only '%'.
1896-
*
1897-
* Back the pin up one char so that we catch the null check
1898-
* the next time through the loop.*/
1899-
pin--;
1890+
pend = pin + flen;
1891+
while (pin != pend) {
1892+
ch = *pin++;
1893+
if (ch != '%' || pin == pend) {
19001894
ptoappend = pin - 1;
19011895
ntoappend = 1;
19021896
}
19031897
/* A % has been seen and ch is the character after it. */
1904-
else if (ch == 'z') {
1898+
else if ((ch = *pin++) == 'z') {
19051899
/* %z -> +HHMM */
19061900
if (zreplacement == NULL) {
19071901
zreplacement = make_somezreplacement(object, "", tzinfoarg);
@@ -2035,12 +2029,10 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
20352029
assert(usednew <= totalnew);
20362030
} /* end while() */
20372031

2038-
if (_PyBytes_Resize(&newfmt, usednew) < 0)
2039-
goto Done;
20402032
{
20412033
PyObject *format;
20422034

2043-
format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt));
2035+
format = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(newfmt), usednew);
20442036
if (format != NULL) {
20452037
result = PyObject_CallFunctionObjArgs(strftime,
20462038
format, timetuple, NULL);

‎Modules/timemodule.c‎

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -787,16 +787,14 @@ time_strftime(PyObject *module, PyObject *args)
787787
PyObject *format;
788788
#endif
789789
PyObject *format_arg;
790+
Py_ssize_t fmtsize;
790791
size_t fmtlen, buflen;
791792
time_char *outbuf = NULL;
792-
size_t i;
793+
size_t outsize, outpos;
793794
PyObject *ret = NULL;
794795

795796
memset((void *) &buf, '\0', sizeof(buf));
796797

797-
/* Will always expect a unicode string to be passed as format.
798-
Given that there's no str type anymore in py3k this seems safe.
799-
*/
800798
if (!PyArg_ParseTuple(args, "U|O:strftime", &format_arg, &tup))
801799
return NULL;
802800

@@ -835,7 +833,7 @@ time_strftime(PyObject *module, PyObject *args)
835833
buf.tm_isdst = 1;
836834

837835
#ifdef HAVE_WCSFTIME
838-
format = PyUnicode_AsWideCharString(format_arg, NULL);
836+
format = PyUnicode_AsWideCharString(format_arg, &fmtsize);
839837
if (format == NULL)
840838
return NULL;
841839
fmt = format;
@@ -845,35 +843,36 @@ time_strftime(PyObject *module, PyObject *args)
845843
if (format == NULL)
846844
return NULL;
847845
fmt = PyBytes_AS_STRING(format);
846+
fmtsize = PyBytes_GET_SIZE(format);
848847
#endif
849848

850849
#if defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME)
851850
/* check that the format string contains only valid directives */
852-
for (outbuf = strchr(fmt, '%');
853-
outbuf != NULL;
854-
outbuf = strchr(outbuf+2, '%'))
851+
for (const time_char *f = memchr(fmt, '%', fmtsize);
852+
f != NULL;
853+
f = memchr(f + 2, '%', fmtsize - (f + 2 - fmt)))
855854
{
856-
if (outbuf[1] == '#')
857-
++outbuf; /* not documented by python, */
858-
if (outbuf[1] == '\0')
855+
if (f[1] == '#')
856+
++f; /* not documented by python, */
857+
if (f + 1 >= fmt + fmtsize)
859858
break;
860-
if ((outbuf[1] == 'y') && buf.tm_year < 0) {
859+
if ((f[1] == 'y') && buf.tm_year < 0) {
861860
PyErr_SetString(PyExc_ValueError,
862861
"format %y requires year >= 1900 on Windows");
863862
Py_DECREF(format);
864863
return NULL;
865864
}
866865
}
867866
#elif (defined(_AIX) || (defined(__sun) && defined(__SVR4))) && defined(HAVE_WCSFTIME)
868-
for (outbuf = wcschr(fmt, '%');
869-
outbuf != NULL;
870-
outbuf = wcschr(outbuf+2, '%'))
867+
for (const time_char *f = wmemchr(fmt, '%', fmtsize);
868+
f != NULL;
869+
f = wmemchr(f + 2, '%', fmtsize - (f + 2 - fmt)))
871870
{
872-
if (outbuf[1] == L'\0')
871+
if (f + 1 >= fmt + fmtsize)
873872
break;
874873
/* Issue #19634: On AIX, wcsftime("y", (1899, 1, 1, 0, 0, 0, 0, 0, 0))
875874
returns "0/" instead of "99" */
876-
if (outbuf[1] == L'y' && buf.tm_year < 0) {
875+
if (f[1] == L'y' && buf.tm_year < 0) {
877876
PyErr_SetString(PyExc_ValueError,
878877
"format %y requires year >= 1900 on AIX");
879878
PyMem_Free(format);
@@ -882,13 +881,14 @@ time_strftime(PyObject *module, PyObject *args)
882881
}
883882
#endif
884883

885-
fmtlen = time_strlen(fmt);
886-
887884
/* I hate these functions that presume you know how big the output
888885
* will be ahead of time...
889886
*/
890-
for (i = 1024; ; i += i) {
891-
outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char));
887+
outsize = fmtsize + 128;
888+
outpos = 0;
889+
fmtlen = time_strlen(fmt);
890+
while (1) {
891+
outbuf = (time_char *)PyMem_Realloc(outbuf, outsize*sizeof(time_char));
892892
if (outbuf == NULL) {
893893
PyErr_NoMemory();
894894
break;
@@ -897,32 +897,41 @@ time_strftime(PyObject *module, PyObject *args)
897897
errno = 0;
898898
#endif
899899
_Py_BEGIN_SUPPRESS_IPH
900-
buflen = format_time(outbuf, i, fmt, &buf);
900+
buflen = format_time(outbuf + outpos, outsize - outpos, fmt, &buf);
901901
_Py_END_SUPPRESS_IPH
902902
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
903903
/* VisualStudio .NET 2005 does this properly */
904904
if (buflen == 0 && errno == EINVAL) {
905905
PyErr_SetString(PyExc_ValueError, "Invalid format string");
906-
PyMem_Free(outbuf);
907906
break;
908907
}
909908
#endif
910-
if (buflen > 0 || i >= 256 * fmtlen) {
911-
/* If the buffer is 256 times as long as the format,
912-
it's probably not failing for lack of room!
913-
More likely, the format yields an empty result,
914-
e.g. an empty format, or %Z when the timezone
915-
is unknown. */
909+
if (buflen == 0 && outsize - outpos < 256 * fmtlen) {
910+
outsize += outsize;
911+
continue;
912+
}
913+
/* If the buffer is 256 times as long as the format,
914+
it's probably not failing for lack of room!
915+
More likely, the format yields an empty result,
916+
e.g. an empty format, or %Z when the timezone
917+
is unknown. */
918+
outpos += buflen + 1;
919+
if (fmtlen < (size_t)fmtsize) {
920+
/* It was not terminating NUL, but an embedded NUL.
921+
Skip the NUL and continue. */
922+
fmt += fmtlen + 1;
923+
fmtsize -= fmtlen + 1;
924+
fmtlen = time_strlen(fmt);
925+
continue;
926+
}
916927
#ifdef HAVE_WCSFTIME
917-
ret = PyUnicode_FromWideChar(outbuf, buflen);
928+
ret = PyUnicode_FromWideChar(outbuf, outpos - 1);
918929
#else
919-
ret = PyUnicode_DecodeLocaleAndSize(outbuf, buflen, "surrogateescape");
930+
ret = PyUnicode_DecodeLocaleAndSize(outbuf, outpos - 1, "surrogateescape");
920931
#endif
921-
PyMem_Free(outbuf);
922-
break;
923-
}
924-
PyMem_Free(outbuf);
932+
break;
925933
}
934+
PyMem_Free(outbuf);
926935
#ifdef HAVE_WCSFTIME
927936
PyMem_Free(format);
928937
#else

0 commit comments

Comments
 (0)