fix: copy the string returned by getenv (#3264)

This commit is contained in:
Marcin Serwin authored and GitHub committed 2025-02-18 07:54:13 +09:00
1 parent 70e72abea3
commit f3d4c1165d
2 files changed
+13 -2

No files matched your search

+5 -2
View File
@@ -1474,8 +1474,10 @@ static time_t my_mktime(struct tm *tm) {
if (tz != NULL) if (tz != NULL)
setenv("TZ", "", 1); setenv("TZ", "", 1);
time_t t = mktime(tm); time_t t = mktime(tm);
if (tz != NULL) if (tz != NULL) {
setenv("TZ", tz, 1); setenv("TZ", tz, 1);
free(tz);
}
return t; return t;
#endif #endif
} }
@@ -1762,13 +1764,14 @@ static jv f_strftime(jq_state *jq, jv a, jv b) {
/* Apple Libc (as of version 1669.40.2) contains a bug which causes it to /* Apple Libc (as of version 1669.40.2) contains a bug which causes it to
* ignore the `tm.tm_gmtoff` in favor of the global timezone. To print the * ignore the `tm.tm_gmtoff` in favor of the global timezone. To print the
* proper timezone offset we temporarily switch the TZ to UTC. */ * proper timezone offset we temporarily switch the TZ to UTC. */
char *tz = getenv("TZ"); char *tz = (tz = getenv("TZ")) != NULL ? strdup(tz) : NULL;
setenv("TZ", "UTC", 1); setenv("TZ", "UTC", 1);
#endif #endif
size_t n = strftime(buf, alloced, fmt, &tm); size_t n = strftime(buf, alloced, fmt, &tm);
#ifdef __APPLE__ #ifdef __APPLE__
if (tz) { if (tz) {
setenv("TZ", tz, 1); setenv("TZ", tz, 1);
free(tz);
} else { } else {
unsetenv("TZ"); unsetenv("TZ");
} }
+8
View File
@@ -728,6 +728,14 @@ if ! $msys && ! $mingw; then
echo "Incorrectly formatted universal time" echo "Incorrectly formatted universal time"
exit 1 exit 1
fi fi
if ! r=$(TZ=Etc/GMT+7 $JQ -nc '1731627341 | .,. | [strftime("%FT%T"),strflocaltime("%FT%T%z")]'
) \
|| [ "$r" != '["2024-11-14T23:35:41","2024-11-14T16:35:41-0700"]
["2024-11-14T23:35:41","2024-11-14T16:35:41-0700"]' ]; then
echo "strftime or strftimelocal are not pure functions"
exit 1
fi
fi fi
exit 0 exit 0