diff options
author | Alexander Monakov <amonakov@ispras.ru> | 2017-09-03 22:12:20 +0300 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2017-09-04 15:55:05 -0400 |
commit | 8e932792c917d11545c2953b35159149f7411eca (patch) | |
tree | 330b20d848bf624c9b7a00c1c0ae0181e3b85e93 /src/env/getenv.c | |
parent | 39db00afadc9d8d0456c46eab42b8cb8ff9f375c (diff) | |
download | musl-8e932792c917d11545c2953b35159149f7411eca.tar.gz |
overhaul environment functions
Rewrite environment access functions to slim down code, fix bugs and
avoid invoking undefined behavior.
* avoid using int-typed iterators where size_t would be correct;
* use strncmp instead of memcmp consistently;
* tighten prologues by invoking __strchrnul;
* handle NULL environ.
putenv:
* handle "=value" input via unsetenv too (will return -1/EINVAL);
* rewrite and simplify __putenv; fix the leak caused by failure to
deallocate entry added by preceding setenv when called from putenv.
setenv:
* move management of libc-allocated entries to this translation unit,
and use no-op weak symbols in putenv/unsetenv;
unsetenv:
* rewrite; this fixes UB caused by testing a free'd pointer against
NULL on entry to subsequent loops.
Not changed:
Failure to extend allocation tracking array (previously __env_map, now
env_alloced) is ignored rather than causing to report -1/ENOMEM to the
caller; the worst-case consequence is leaking this allocation when it
is removed or replaced in a subsequent environment access.
Initially UB in unsetenv was reported by Alexander Cherepanov.
Using a weak alias to avoid pulling in malloc via unsetenv was
suggested by Rich Felker.
Diffstat (limited to 'src/env/getenv.c')
-rw-r--r-- | src/env/getenv.c | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/env/getenv.c b/src/env/getenv.c index 00c1bce0..cf34672c 100644 --- a/src/env/getenv.c +++ b/src/env/getenv.c @@ -2,13 +2,14 @@ #include <string.h> #include "libc.h" +char *__strchrnul(const char *, int); + char *getenv(const char *name) { - int i; - size_t l = strlen(name); - if (!__environ || !*name || strchr(name, '=')) return NULL; - for (i=0; __environ[i] && (strncmp(name, __environ[i], l) - || __environ[i][l] != '='); i++); - if (__environ[i]) return __environ[i] + l+1; - return NULL; + size_t l = __strchrnul(name, '=') - name; + if (l && !name[l] && __environ) + for (char **e = __environ; *e; e++) + if (!strncmp(name, *e, l) && l[*e] == '=') + return *e + l+1; + return 0; } |