[tz] source code question regarding localtime.c PS
Alan Barrett
apb at cequrux.com
Wed Aug 7 15:38:54 UTC 2013
On Wed, 07 Aug 2013, Clive D.W. Feather wrote:
>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.
[tcycles has type time_t, which might not be an integer type]
[icycles has type int_fast64_t]
>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.
If the value is out of range, then the assignment (icycles =
tcycles) invokes undefined behaviour, so the test in the if
statement might not do what one would expect.
If the tz code wants to check that a variable is in range, I think
it should do without performing a possibly-undefined operation.
--apb (Alan Barrett)
More information about the tz
mailing list