]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
setenv.c: Get rid of alloca.
authorJoe Simmons-Talbott <josimmon@redhat.com>
Fri, 30 Jun 2023 14:31:45 +0000 (14:31 +0000)
committerJoe Simmons-Talbott <josimmon@redhat.com>
Fri, 30 Jun 2023 14:31:45 +0000 (14:31 +0000)
Use malloc rather than alloca to avoid potential stack overflow.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
stdlib/setenv.c

index ba5257d3bfafd5227ac03e09f96de5acb551f685..cc71287fcc2726701ac13cbfc402979e06a0d29d 100644 (file)
@@ -182,18 +182,11 @@ __add_to_environ (const char *name, const char *value, const char *combined,
        {
          const size_t varlen = namelen + 1 + vallen;
 #ifdef USE_TSEARCH
-         char *new_value;
-         int use_alloca = __libc_use_alloca (varlen);
-         if (__builtin_expect (use_alloca, 1))
-           new_value = (char *) alloca (varlen);
-         else
+         char *new_value = malloc (varlen);
+         if (new_value == NULL)
            {
-             new_value = malloc (varlen);
-             if (new_value == NULL)
-               {
-                 UNLOCK;
-                 return -1;
-               }
+             UNLOCK;
+             return -1;
            }
 # ifdef _LIBC
          __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
@@ -209,35 +202,14 @@ __add_to_environ (const char *name, const char *value, const char *combined,
 #endif
            {
 #ifdef USE_TSEARCH
-             if (__glibc_unlikely (! use_alloca))
-               np = new_value;
-             else
+             np = new_value;
 #endif
-               {
-                 np = malloc (varlen);
-                 if (__glibc_unlikely (np == NULL))
-                   {
-                     UNLOCK;
-                     return -1;
-                   }
-
-#ifdef USE_TSEARCH
-                 memcpy (np, new_value, varlen);
-#else
-                 memcpy (np, name, namelen);
-                 np[namelen] = '=';
-                 memcpy (&np[namelen + 1], value, vallen);
-#endif
-               }
              /* And remember the value.  */
              STORE_VALUE (np);
            }
 #ifdef USE_TSEARCH
          else
-           {
-             if (__glibc_unlikely (! use_alloca))
-               free (new_value);
-           }
+           free (new_value);
 #endif
        }