zdump loop problem

Paul Eggert eggert at CS.UCLA.EDU
Mon Nov 24 17:54:55 UTC 2008


Robbin Kawabata <Robbin.Kawabata at sun.com> writes:

>                         if (t >= cuthitime)
>                                 break;
> +                       /* check if newt will overrun maximum time_t value */
> +                       if (t > LONG_MAX - (SECSPERHOUR * 12))
> +                               break;
>                         newt = t + SECSPERHOUR * 12;
>                         if (newt >= cuthitime)
>                                 break;
> -                       if (newt <= t)
> -                               break;

Thanks for the diagnosis and patch.  I see one problem with that code,
though: it assumes that time_t == long, which is not a portable
assumption.  Also, the code is overly complicated.  I suggest using
something like this instead:

             if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
                     break;
             newt = t + SECSPERHOUR * 12;

The seemingly redundant "t >= cuthitime" test prevents arithmetic
overflow in the subtraction (admittedly highly unlikely, but I couldn't
prove to my own satisfaction that it could never occur).  If we are
really worried about cycles, the seemingly-redundant test could be
hoisted out of the loop, as it applies only to the first iteration of
the loop.



More information about the tz mailing list