aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-07-22 02:15:17 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2017-07-22 02:15:17 +0200
commitef0366eb4f02bb0cda359b977fdbdfa7c145f76f (patch)
tree0ecf3f34435376a663b09f836c0938a108ffb1a2 /libbb
parentc9e7843dde3555aea0318b01a428a47b35cf2df4 (diff)
downloadbusybox-ef0366eb4f02bb0cda359b977fdbdfa7c145f76f.tar.gz
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>
Diffstat (limited to 'libbb')
-rw-r--r--libbb/xfuncs_printf.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index 1b11caf6b..f569b0263 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -344,20 +344,28 @@ void FAST_FUNC xsetenv(const char *key, const char *value)
*/
void FAST_FUNC bb_unsetenv(const char *var)
{
- char *tp = strchr(var, '=');
-
- if (!tp) {
- unsetenv(var);
- return;
+ char onstack[128 - 16]; /* smaller stack setup code on x86 */
+ char *tp;
+
+ tp = strchr(var, '=');
+ if (tp) {
+ /* 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);
+ }
}
-
- /* 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);
+ unsetenv(var);
free(tp);
}