@@ -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