[tz] source code question regarding localtime.c PS

Clive D.W. Feather clive at davros.org
Wed Aug 7 16:51:17 UTC 2013

Paul Eggert said:
> Thanks for mentioning the problem.  The code relies on undefined
> behavior when a time_t value is outside int_fast64_t range.
> It's better to avoid undefined behavior,

> +	if (!TYPE_INTEGRAL(time_t)) {
> +		if (INTMAX_MIN < t && t < INTMAX_MAX) {

In principle UINTMAX_MAX could be larger than the largest value
representable in time_t if it is (say) float. If so, the implicit
conversion to float is undefined and you haven't solved the problem. You
need to compare the various limits to decide which type can handle all the
values of the other.

Of course, there's no requirement that time_t values map to times in a
linear manner - for example, an integral time_t could be split into bit
fields holding hours, minutes, seconds, etc. That's why difftime() exists.

Clive D.W. Feather          | If you lie to the compiler,
Email: clive at davros.org     | it will get its revenge.
Web: http://www.davros.org  |   - Henry Spencer
Mobile: +44 7973 377646

More information about the tz mailing list