libbb: avoid malloc/free in bb_unsetenv()
function old new delta bb_unsetenv 55 83 +28 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
c9e7843dde
commit
ef0366eb4f
@ -344,20 +344,28 @@ void FAST_FUNC xsetenv(const char *key, const char *value)
|
|||||||
*/
|
*/
|
||||||
void FAST_FUNC bb_unsetenv(const char *var)
|
void FAST_FUNC bb_unsetenv(const char *var)
|
||||||
{
|
{
|
||||||
char *tp = strchr(var, '=');
|
char onstack[128 - 16]; /* smaller stack setup code on x86 */
|
||||||
|
char *tp;
|
||||||
|
|
||||||
if (!tp) {
|
tp = strchr(var, '=');
|
||||||
unsetenv(var);
|
if (tp) {
|
||||||
return;
|
/* In case var was putenv'ed, we can't replace '='
|
||||||
|
* with NUL and unsetenv(var) - it won't work,
|
||||||
|
* env is modified by the replacement, unsetenv
|
||||||
|
* sees "VAR" instead of "VAR=VAL" and does not remove it!
|
||||||
|
* Horror :(
|
||||||
|
*/
|
||||||
|
unsigned sz = tp - var;
|
||||||
|
if (sz < sizeof(onstack)) {
|
||||||
|
((char*)mempcpy(onstack, var, sz))[0] = '\0';
|
||||||
|
tp = NULL;
|
||||||
|
var = onstack;
|
||||||
|
} else {
|
||||||
|
/* unlikely: very long var name */
|
||||||
|
var = tp = xstrndup(var, sz);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
unsetenv(var);
|
||||||
/* In case var was putenv'ed, we can't replace '='
|
|
||||||
* with NUL and unsetenv(var) - it won't work,
|
|
||||||
* env is modified by the replacement, unsetenv
|
|
||||||
* sees "VAR" instead of "VAR=VAL" and does not remove it!
|
|
||||||
* horror :( */
|
|
||||||
tp = xstrndup(var, tp - var);
|
|
||||||
unsetenv(tp);
|
|
||||||
free(tp);
|
free(tp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user