diff options
author | Joe Simmons-Talbott <josimmon@redhat.com> | 2023-05-03 12:40:05 -0400 |
---|---|---|
committer | Siddhesh Poyarekar <siddhesh@sourceware.org> | 2023-05-05 10:55:48 -0400 |
commit | 21f0b087ee10391433d8279e7c6f104fb9ea0eef (patch) | |
tree | 99ceac5eddee45885884323d01d492f682bcb144 /time | |
parent | 642f1b9b3de8d847b43af928107057116eb6e7f1 (diff) | |
download | glibc-21f0b087ee10391433d8279e7c6f104fb9ea0eef.tar.gz glibc-21f0b087ee10391433d8279e7c6f104fb9ea0eef.tar.xz glibc-21f0b087ee10391433d8279e7c6f104fb9ea0eef.zip |
time: Remove alloca() from getdate
Reduce the usage of alloca() to the bare minimum to avoid the potential for stack overflow. Use __strndup to simplify the code. Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
Diffstat (limited to 'time')
-rw-r--r-- | time/getdate.c | 23 |
1 files changed, 5 insertions, 18 deletions
diff --git a/time/getdate.c b/time/getdate.c index c5c8378707..1dcbd77188 100644 --- a/time/getdate.c +++ b/time/getdate.c @@ -26,7 +26,6 @@ #include <unistd.h> #include <sys/stat.h> #include <ctype.h> -#include <alloca.h> #define TM_YEAR_BASE 1900 @@ -153,26 +152,14 @@ __getdate_r (const char *string, struct tm *tp) if (inlen < oldlen) { - bool using_malloc = false; - - if (__libc_use_alloca (inlen + 1)) - instr = alloca (inlen + 1); - else + instr = __strndup(string, inlen); + if (instr == NULL) { - instr = malloc (inlen + 1); - if (instr == NULL) - { - fclose (fp); - return 6; - } - using_malloc = true; + fclose(fp); + return 6; } - memcpy (instr, string, inlen); - instr[inlen] = '\0'; - string = instr; - if (!using_malloc) - instr = NULL; + string = instr; } line = NULL; |