You can't pass a NULL value to setenv(3). The "put things back how they were"
intent of this code is best achieved by calling unsetenv(3) in the NULL case.

(This causes a crash with "hwclock -u" on Android. glibc silently corrupts the
environment instead.)
---
 lib/xwrap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/xwrap.c b/lib/xwrap.c
index 6d0c511..8a3ed6c 100644
--- a/lib/xwrap.c
+++ b/lib/xwrap.c
@@ -704,13 +704,13 @@ void xregcomp(regex_t *preg, char *regex, int cflags)

 char *xtzset(char *new)
 {
-  char *tz = getenv("TZ");
+  char *old = getenv("TZ");

-  if (tz) tz = xstrdup(tz);
-  if (setenv("TZ", new, 1)) perror_exit("setenv");
+  if (old) old = xstrdup(old);
+  if (new ? setenv("TZ", new, 1) : unsetenv("TZ")) perror_exit("setenv");
   tzset();

-  return tz;
+  return old;
 }

 // Set a signal handler
-- 
2.6.0.rc2.230.g3dd15c0
From 0abd2ad442f553c78dda722c616d2a60cb699060 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Sat, 9 Jan 2016 12:20:50 -0800
Subject: [PATCH] Fix hwclock -u.

You can't pass a NULL value to setenv(3). The "put things back how they were"
intent of this code is best achieved by calling unsetenv(3) in the NULL case.

(This causes a crash with "hwclock -u" on Android. glibc silently corrupts the
environment instead.)
---
 lib/xwrap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/xwrap.c b/lib/xwrap.c
index 6d0c511..8a3ed6c 100644
--- a/lib/xwrap.c
+++ b/lib/xwrap.c
@@ -704,13 +704,13 @@ void xregcomp(regex_t *preg, char *regex, int cflags)
 
 char *xtzset(char *new)
 {
-  char *tz = getenv("TZ");
+  char *old = getenv("TZ");
 
-  if (tz) tz = xstrdup(tz);
-  if (setenv("TZ", new, 1)) perror_exit("setenv");
+  if (old) old = xstrdup(old);
+  if (new ? setenv("TZ", new, 1) : unsetenv("TZ")) perror_exit("setenv");
   tzset();
 
-  return tz;
+  return old;
 }
 
 // Set a signal handler
-- 
2.6.0.rc2.230.g3dd15c0

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to