[tz] source code question regarding localtime.c PS

Clive D.W. Feather clive at davros.org
Wed Aug 7 14:27:23 UTC 2013


Alois Treindl said:
> In localtime.c, function localsub() are these three lines of code:
> 
> 1295    icycles = tcycles;
> 1296    if (tcycles - icycles >= 1 || icycles - tcycles >=  1)
> 1297      return NULL;
> 
> I do not understand the reason why lines 1296 and 1297 exist.
> icycles and tcycles are equal.

time_t just has to be an arithmetic type. This could include long long,
double, long double, or even a complex type. Therefore it's possible that
tcycles could contain a value that's not in the range of int_fast64_t
(the type of icycles).

That code checks whether the value in tcycles is within the range of
int_fast64_t. If it is, the conversion on line 1295 will either produce the
same number or (if time_t is floating point) will round it off to the
nearest integer. In that case, both halves of the test will be false.

But if the value in tcycles is out of range, the conversion will generate a
completely different number, and so one of those tests will be true.

-- 
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