mirror of
git://sourceware.org/git/glibc.git
synced 2025-03-06 20:58:33 +01:00
setenv.c: Get rid of alloca.
Use malloc rather than alloca to avoid potential stack overflow. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
This commit is contained in:
parent
9555be54ef
commit
9401024e5e
1 changed files with 6 additions and 34 deletions
|
@ -182,18 +182,11 @@ __add_to_environ (const char *name, const char *value, const char *combined,
|
||||||
{
|
{
|
||||||
const size_t varlen = namelen + 1 + vallen;
|
const size_t varlen = namelen + 1 + vallen;
|
||||||
#ifdef USE_TSEARCH
|
#ifdef USE_TSEARCH
|
||||||
char *new_value;
|
char *new_value = malloc (varlen);
|
||||||
int use_alloca = __libc_use_alloca (varlen);
|
if (new_value == NULL)
|
||||||
if (__builtin_expect (use_alloca, 1))
|
|
||||||
new_value = (char *) alloca (varlen);
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
new_value = malloc (varlen);
|
UNLOCK;
|
||||||
if (new_value == NULL)
|
return -1;
|
||||||
{
|
|
||||||
UNLOCK;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
# ifdef _LIBC
|
# ifdef _LIBC
|
||||||
__mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
|
__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
|
#endif
|
||||||
{
|
{
|
||||||
#ifdef USE_TSEARCH
|
#ifdef USE_TSEARCH
|
||||||
if (__glibc_unlikely (! use_alloca))
|
np = new_value;
|
||||||
np = new_value;
|
|
||||||
else
|
|
||||||
#endif
|
#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. */
|
/* And remember the value. */
|
||||||
STORE_VALUE (np);
|
STORE_VALUE (np);
|
||||||
}
|
}
|
||||||
#ifdef USE_TSEARCH
|
#ifdef USE_TSEARCH
|
||||||
else
|
else
|
||||||
{
|
free (new_value);
|
||||||
if (__glibc_unlikely (! use_alloca))
|
|
||||||
free (new_value);
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue