[patch] timegm.3: copy the string returned by getenv()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The example of a portable version of timegm() uses the string returned by
getenv() after calling setenv() on the same environment variale. The tz
string may be invalid as per getenv.3:

"The string pointed to by the return value of getenv() may be statically
allocated, and can be modified by a subsequent call to getenv(), putenv(3),
setenv(3), or unsetenv(3)."

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@xxxxxxxxxxxx>
---
 man3/timegm.3 | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/man3/timegm.3 b/man3/timegm.3
index 3ab194b..0e71be5 100644
--- a/man3/timegm.3
+++ b/man3/timegm.3
@@ -84,12 +84,15 @@ my_timegm(struct tm *tm)
     char *tz;
 
     tz = getenv("TZ");
+    if (tz)
+        tz = strdup(tz);
     setenv("TZ", "", 1);
     tzset();
     ret = mktime(tm);
-    if (tz)
+    if (tz) {
         setenv("TZ", tz, 1);
-    else
+        free(tz);
+    } else
         unsetenv("TZ");
     tzset();
     return ret;
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux