From 2e8b0d97e2fcc74d12be574753ea871172e1999d Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 12 Dec 2013 18:03:04 -0600 Subject: [PATCH] strftime should also return zero if the resulting string is truncated and/or not properly NUL terminated --- libc/time/lib_strftime.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/libc/time/lib_strftime.c b/libc/time/lib_strftime.c index 81d63a627d..157687910d 100644 --- a/libc/time/lib_strftime.c +++ b/libc/time/lib_strftime.c @@ -136,9 +136,9 @@ static const char * const g_monthname[12] = * * Returned Value: * The strftime() function returns the number of characters placed in the - * array s, not including the terminating null byte, provided the string, - * including the terminating null byte, fits. Otherwise, it returns 0, - * and the contents of the array is undefined. + * array s, not including the terminating null byte, provided the string, + * including the terminating null byte, fits. Otherwise, it returns 0, + * and the contents of the array is undefined. * ****************************************************************************/ @@ -394,12 +394,30 @@ size_t strftime(char *s, size_t max, const char *format, const struct tm *tm) chleft -= len; } - /* Append terminating null byte (if there is space for it) */ + /* We get here because either we have reached the end of the format string + * or because there is no more space in the user-provided buffer and the + * resulting string has been truncated. + * + * Is there space remaining in the user-provided buffer for the NUL + * terminator? + */ if (chleft > 0) { + /* Yes, append terminating NUL byte */ + *dest = '\0'; + + /* And return the number of bytes in the resulting string (excluding + * the NUL terminator). + */ + + return max - chleft; } - return max - chleft; + /* The string was truncated and/or not properly terminated. Return + * zero. + */ + + return 0; }