Hi Paul,
On 10/11/21 5:40 PM, Paul Eggert wrote:
On 10/11/21 4:12 AM, Alejandro Colomar wrote:
time_t portable_timegm(struct tm *tm)
{
tm->tm_isdst = 0;
/*
* If another thread modifies the timezone during the
* execution of the line below, it will produce undefined
* behavior.
*/
return mktime(tm) - timezone;
}
This doesn't work for multiple reasons:
it's not thread-safe,
Actually, since timegm(3) is implemented in terms of mktime(3), as far
as I could read from glibc code, the problem will be the same, I think.
I don't understand why it wasn't the other way around; maybe it was
more complex internally... But timegm(3) shouldn't need to depend on
environment variables.
mktime might set timezone even in a single-threaded app,
Yes, I should have called tzset() before the return line.
and the subtraction might overflow.
Yup, casting to int64_t needed. BTW, I had a look at mktime source
code, and it uses long, which might be 32 bits, and then there's a lot
of checking for overflow. Wouldn't it be simpler to just implement
mktime(3) with int64_t internally? Then, only at the return, cast it
implicitly to whatever time_t means, but int64_t would simplify the code
very much, I think.
Thanks,
Alex
--
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/