diff mbox series

time: Remove alloca() from getdate

Message ID 20230426135717.2512491-1-josimmon@redhat.com
State New
Headers show
Series time: Remove alloca() from getdate | expand

Commit Message

Joe Simmons-Talbott April 26, 2023, 1:57 p.m. UTC
Reduce the usage of alloca() to the bare minimum to avoid the potential
for stack overflow.  Use __strndup to simplify the code.

---
 time/getdate.c | 21 +--------------------
 1 file changed, 1 insertion(+), 20 deletions(-)

Comments

Joe Simmons-Talbott April 26, 2023, 3:20 p.m. UTC | #1
On Wed, Apr 26, 2023 at 09:57:17AM -0400, Joe Simmons-Talbott wrote:
> Reduce the usage of alloca() to the bare minimum to avoid the potential
> for stack overflow.  Use __strndup to simplify the code.
> 
> ---
>  time/getdate.c | 21 +--------------------
>  1 file changed, 1 insertion(+), 20 deletions(-)
> 
> diff --git a/time/getdate.c b/time/getdate.c
> index c5c8378707..204308da1e 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,8 @@ __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 = malloc (inlen + 1);
> -	  if (instr == NULL)
> -	    {
> -	      fclose (fp);
> -	      return 6;
> -	    }
> -	  using_malloc = true;
> -	}
> -      memcpy (instr, string, inlen);
> -      instr[inlen] = '\0';
> +      instr = __strndup(string, inlen);

I neglected to add error handling for __strndup().  v2 coming shortly.

Thanks,
Joe
diff mbox series

Patch

diff --git a/time/getdate.c b/time/getdate.c
index c5c8378707..204308da1e 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,8 @@  __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 = malloc (inlen + 1);
-	  if (instr == NULL)
-	    {
-	      fclose (fp);
-	      return 6;
-	    }
-	  using_malloc = true;
-	}
-      memcpy (instr, string, inlen);
-      instr[inlen] = '\0';
+      instr = __strndup(string, inlen);
       string = instr;
-
-      if (!using_malloc)
-	instr = NULL;
     }
 
   line = NULL;