* [patch] timegm.3: copy the string returned by getenv()
@ 2013-06-26 15:08 Jérémie Galarneau
[not found] ` <1372259329-26397-1-git-send-email-jeremie.galarneau-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org>
0 siblings, 1 reply; 2+ messages in thread
From: Jérémie Galarneau @ 2013-06-26 15:08 UTC (permalink / raw)
To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w
Cc: linux-man-u79uwXL29TY76Z2rM5mHXA, Jérémie Galarneau
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@efficios.com>
---
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-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2013-06-26 22:30 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-06-26 15:08 [patch] timegm.3: copy the string returned by getenv() Jérémie Galarneau
[not found] ` <1372259329-26397-1-git-send-email-jeremie.galarneau-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org>
2013-06-26 22:30 ` Michael Kerrisk
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).