diff options
author | Gregory Nutt <gnutt@nuttx.org> | 2013-12-12 18:03:04 -0600 |
---|---|---|
committer | Gregory Nutt <gnutt@nuttx.org> | 2013-12-12 18:03:04 -0600 |
commit | b47637171ea85e8fb1b97a3533d18267771faaa6 (patch) | |
tree | dc45a1813e61ad7c31e147d2232b2ef0c801d562 | |
parent | 1f87e2b12a8ae387f184253e6d89dea0af7ea721 (diff) | |
download | px4-nuttx-b47637171ea85e8fb1b97a3533d18267771faaa6.tar.gz px4-nuttx-b47637171ea85e8fb1b97a3533d18267771faaa6.tar.bz2 px4-nuttx-b47637171ea85e8fb1b97a3533d18267771faaa6.zip |
strftime should also return zero if the resulting string is truncated and/or not properly NUL terminated
-rw-r--r-- | nuttx/libc/time/lib_strftime.c | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/nuttx/libc/time/lib_strftime.c b/nuttx/libc/time/lib_strftime.c index 81d63a627..157687910 100644 --- a/nuttx/libc/time/lib_strftime.c +++ b/nuttx/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; } |