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>
This commit is contained in:
Joe Simmons-Talbott 2023-05-03 12:40:05 -04:00 committed by Siddhesh Poyarekar
parent 642f1b9b3d
commit 21f0b087ee

View File

@ -26,7 +26,6 @@
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <ctype.h> #include <ctype.h>
#include <alloca.h>
#define TM_YEAR_BASE 1900 #define TM_YEAR_BASE 1900
@ -153,26 +152,14 @@ __getdate_r (const char *string, struct tm *tp)
if (inlen < oldlen) if (inlen < oldlen)
{ {
bool using_malloc = false; instr = __strndup(string, inlen);
if (__libc_use_alloca (inlen + 1))
instr = alloca (inlen + 1);
else
{
instr = malloc (inlen + 1);
if (instr == NULL) if (instr == NULL)
{ {
fclose (fp); fclose(fp);
return 6; return 6;
} }
using_malloc = true;
}
memcpy (instr, string, inlen);
instr[inlen] = '\0';
string = instr;
if (!using_malloc) string = instr;
instr = NULL;
} }
line = NULL; line = NULL;