This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PING^6][BZ #15894][PATCH] Deduplicate setenv.c
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: Mike Frysinger <vapier at gentoo dot org>
- Cc: libc-alpha at sourceware dot org
- Date: Mon, 10 Feb 2014 13:59:19 +0100
- Subject: Re: [PING^6][BZ #15894][PATCH] Deduplicate setenv.c
- Authentication-results: sourceware.org; auth=none
- References: <20131015093450 dot GA1459 at domone dot podge> <20131204113438 dot GA970 at domone dot podge> <20140208002308 dot GG32075 at domone dot podge> <1456760 dot YNkU5zRZBf at vapier>
On Sat, Feb 08, 2014 at 09:52:32AM -0500, Mike Frysinger wrote:
> LGTM
> -mike
I commited a sligthly different version as varlen became unused
variable.
diff --git a/ChangeLog b/ChangeLog
index bded2c3..cf1b17d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
2014-02-10 OndÅej BÃlka <neleai@seznam.cz>
+ [BZ #15894]
+ * stdlib/setenv.c (__add_to_environ): Remove duplicate code.
+
+2014-02-10 OndÅej BÃlka <neleai@seznam.cz>
+
* malloc/arena.c (grow_heap, get_free_list, reused_arena,
arena_get2): Remove THREAD_STATS conditionals.
* malloc/malloc.c (__malloc_assert, __libc_realloc, _int_free,
diff --git a/NEWS b/NEWS
index 851167f..0f4b8d4 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,8 @@ Version 2.20
* The following bugs are resolved with this release:
+ 15894.
+
* The am33 port, which had not worked for several years, has been removed
from ports.
diff --git a/stdlib/setenv.c b/stdlib/setenv.c
index 7df5b3f..bb99c2a 100644
--- a/stdlib/setenv.c
+++ b/stdlib/setenv.c
@@ -135,7 +135,6 @@ __add_to_environ (name, value, combined, replace)
if (ep == NULL || __builtin_expect (*ep == NULL, 1))
{
- const size_t varlen = namelen + 1 + vallen;
char **new_environ;
/* We allocated this space; we can extend it. */
@@ -147,81 +146,17 @@ __add_to_environ (name, value, combined, replace)
return -1;
}
- /* If the whole entry is given add it. */
- if (combined != NULL)
- /* We must not add the string to the search tree since it belongs
- to the user. */
- new_environ[size] = (char *) combined;
- else
- {
- /* See whether the value is already known. */
-#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
- {
- new_value = malloc (varlen);
- if (new_value == NULL)
- {
- UNLOCK;
- if (last_environ == NULL)
- free (new_environ);
- return -1;
- }
- }
-# ifdef _LIBC
- __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
- value, vallen);
-# else
- memcpy (new_value, name, namelen);
- new_value[namelen] = '=';
- memcpy (&new_value[namelen + 1], value, vallen);
-# endif
-
- new_environ[size] = KNOWN_VALUE (new_value);
- if (__builtin_expect (new_environ[size] == NULL, 1))
-#endif
- {
-#ifdef USE_TSEARCH
- if (__builtin_expect (! use_alloca, 0))
- new_environ[size] = new_value;
- else
-#endif
- {
- new_environ[size] = (char *) malloc (varlen);
- if (__builtin_expect (new_environ[size] == NULL, 0))
- {
- UNLOCK;
- return -1;
- }
-
-#ifdef USE_TSEARCH
- memcpy (new_environ[size], new_value, varlen);
-#else
- memcpy (new_environ[size], name, namelen);
- new_environ[size][namelen] = '=';
- memcpy (&new_environ[size][namelen + 1], value, vallen);
-#endif
- }
-
- /* And save the value now. We cannot do this when we remove
- the string since then we cannot decide whether it is a
- user string or not. */
- STORE_VALUE (new_environ[size]);
- }
- }
-
if (__environ != last_environ)
memcpy ((char *) new_environ, (char *) __environ,
size * sizeof (char *));
+ new_environ[size] = NULL;
new_environ[size + 1] = NULL;
+ ep = new_environ + size;
last_environ = __environ = new_environ;
}
- else if (replace)
+ if (*ep == NULL || replace)
{
char *np;