Util-Linux package development
 help / color / mirror / Atom feed
* [PATCH] hwclock: avoid UB if TZUTC is unset; include sys/time.h for timeval
@ 2015-04-23  4:49 Isaac Dunham
  2015-04-23 23:45 ` [PATCH 0/1] hwclock: Remove TZUTC J William Piggott
  0 siblings, 1 reply; 5+ messages in thread
From: Isaac Dunham @ 2015-04-23  4:49 UTC (permalink / raw)
  To: util-linux; +Cc: Isaac Dunham

Fix a crash reported by Jean-Marc Pigeon on the musl mailing list.

If TZUTC is unset, setenv() will be called with value = NULL,
which invokes undefined behavior.
With musl libc, this causes a crash.
On uclibc and glibc, this currently happens to act like the long form
here, but glibc will make this an error:
 https://sourceware.org/ml/libc-alpha/2015-03/threads.html#00449

Additionally, include <sys/time.h> to define struct timeval.
---
 sys-utils/hwclock.c | 5 ++++-
 sys-utils/hwclock.h | 1 +
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/sys-utils/hwclock.c b/sys-utils/hwclock.c
index e1e5816..67971f0 100644
--- a/sys-utils/hwclock.c
+++ b/sys-utils/hwclock.c
@@ -408,7 +408,10 @@ mktime_tz(struct tm tm, const bool universal,
 		 * libc's:
 		 *    TZUTC=:/usr/share/zoneinfo/right/UTC
 		 */
-		setenv("TZ", getenv("TZUTC"), TRUE);
+		char *tzutc;
+
+		tzutc = getenv("TZUTC");
+		setenv("TZ", tzutc ? tzutc : "", TRUE);
 		/*
 		 * Note: tzset() gets called implicitly by the time code,
 		 * but only the first time. When changing the environment
diff --git a/sys-utils/hwclock.h b/sys-utils/hwclock.h
index 974d96a..7f19c39 100644
--- a/sys-utils/hwclock.h
+++ b/sys-utils/hwclock.h
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include <sys/time.h>
 
 #include "c.h"
 
-- 
2.3.5


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-04-27  8:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-04-23  4:49 [PATCH] hwclock: avoid UB if TZUTC is unset; include sys/time.h for timeval Isaac Dunham
2015-04-23 23:45 ` [PATCH 0/1] hwclock: Remove TZUTC J William Piggott
2015-04-23 23:53   ` [PATCH 1/1] " J William Piggott
2015-04-27  8:07     ` Karel Zak
2015-04-24  4:26   ` [PATCH 0/1] " Isaac Dunham

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox