From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2206) id B6F123858D33; Fri, 5 May 2023 14:56:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B6F123858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683298583; bh=xDMDYM5qXGGtOfsCHvt9CbHoNn+8IdIhUplfDVcscj4=; h=From:To:Subject:Date:From; b=J3GCuVIWcA8Y6can5tnqwHz7DO2s1gwbxshZGxYdXqIIyxKLzVbHypZc/NOeW8ZCt MrZVjYtIr8fvM3xJfaWBlwtiUCOGnf9Z0SnoGldGyvg+/ytEAaPnzyFvxJrkDfOg/e wClfGiJkpXDXrqyfuWfQuVP0VQmnEaO99PvwYccs= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Siddhesh Poyarekar To: glibc-cvs@sourceware.org Subject: [glibc] time: Remove alloca() from getdate X-Act-Checkin: glibc X-Git-Author: Joe Simmons-Talbott X-Git-Refname: refs/heads/master X-Git-Oldrev: 642f1b9b3de8d847b43af928107057116eb6e7f1 X-Git-Newrev: 21f0b087ee10391433d8279e7c6f104fb9ea0eef Message-Id: <20230505145623.B6F123858D33@sourceware.org> Date: Fri, 5 May 2023 14:56:23 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=21f0b087ee10391433d8279e7c6f104fb9ea0eef commit 21f0b087ee10391433d8279e7c6f104fb9ea0eef Author: Joe Simmons-Talbott Date: Wed May 3 12:40:05 2023 -0400 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 Diff: --- time/getdate.c | 23 +++++------------------ 1 file 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 #include #include -#include #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;