From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7932) id E326F385DC31; Fri, 30 Jun 2023 14:38:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E326F385DC31 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1688135890; bh=/MWbW85Zs3QXYx2wh77Q+Sngj2WzNNkUiytck3Owbbk=; h=From:To:Subject:Date:From; b=B84kA9R83q4oqX70bxrZ9GKoOJ/XpV0Y6nlxnFt67QZHM0gBve+VxafGfRczRkQcC C1n7ZU+Pev5bZ/rpqi+LknpWb0r3nh/cF2pgJorg88DJ+6TtcQryJAUIGwSblA26Gi cmdNaozI/heE2NHGWdHxi7dz1J/fNCUxKbH1DrAw= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Joe Simmons-Talbott To: glibc-cvs@sourceware.org Subject: [glibc] setenv.c: Get rid of alloca. X-Act-Checkin: glibc X-Git-Author: Joe Simmons-Talbott X-Git-Refname: refs/heads/master X-Git-Oldrev: 9555be54ef94e5d017ce4235e4f7c4e16662e17e X-Git-Newrev: 9401024e5e6be0e1c3870e185daae865cd4501f4 Message-Id: <20230630143810.E326F385DC31@sourceware.org> Date: Fri, 30 Jun 2023 14:38:10 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=9401024e5e6be0e1c3870e185daae865cd4501f4 commit 9401024e5e6be0e1c3870e185daae865cd4501f4 Author: Joe Simmons-Talbott Date: Fri Jun 30 14:31:45 2023 +0000 setenv.c: Get rid of alloca. Use malloc rather than alloca to avoid potential stack overflow. Reviewed-by: Adhemerval Zanella Diff: --- stdlib/setenv.c | 40 ++++++---------------------------------- 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/stdlib/setenv.c b/stdlib/setenv.c index ba5257d3bf..cc71287fcc 100644 --- a/stdlib/setenv.c +++ b/stdlib/setenv.c @@ -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 }