@@ -610,25 +610,28 @@ time_strftime(PyObject *self, PyObject *args)
610610
611611#if defined(MS_WINDOWS ) && !defined(HAVE_WCSFTIME )
612612 /* check that the format string contains only valid directives */
613- for (outbuf = strchr (fmt , '%' );
613+ for (outbuf = strchr (fmt , '%' );
614614 outbuf != NULL ;
615615 outbuf = strchr (outbuf + 2 , '%' ))
616616 {
617- if (outbuf [1 ]== '#' )
617+ if (outbuf [1 ] == '#' )
618618 ++ outbuf ; /* not documented by python, */
619- if ((outbuf [1 ] == 'y' ) && buf .tm_year < 0 )
620- {
619+ if (outbuf [1 ] == '\0' )
620+ break ;
621+ if ((outbuf [1 ] == 'y' ) && buf .tm_year < 0 ) {
621622 PyErr_SetString (PyExc_ValueError ,
622623 "format %y requires year >= 1900 on Windows" );
623624 Py_DECREF (format );
624625 return NULL ;
625626 }
626627 }
627628#elif (defined(_AIX ) || defined(sun )) && defined(HAVE_WCSFTIME )
628- for (outbuf = wcschr (fmt , '%' );
629+ for (outbuf = wcschr (fmt , '%' );
629630 outbuf != NULL ;
630631 outbuf = wcschr (outbuf + 2 , '%' ))
631632 {
633+ if (outbuf [1 ] == L'\0' )
634+ break ;
632635 /* Issue #19634: On AIX, wcsftime("y", (1899, 1, 1, 0, 0, 0, 0, 0, 0))
633636 returns "0/" instead of "99" */
634637 if (outbuf [1 ] == L'y' && buf .tm_year < 0 ) {
@@ -659,7 +662,8 @@ time_strftime(PyObject *self, PyObject *args)
659662#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__ )
660663 err = errno ;
661664#endif
662- if (buflen > 0 || i >= 256 * fmtlen ) {
665+ if (buflen > 0 || fmtlen == 0 ||
666+ (fmtlen > 4 && i >= 256 * fmtlen )) {
663667 /* If the buffer is 256 times as long as the format,
664668 it's probably not failing for lack of room!
665669 More likely, the format yields an empty result,
0 commit comments