lfpinit() signed or unsigned?

Gary E. Miller gem at rellim.com
Thu Mar 9 22:54:23 UTC 2017


Yo Eric!

On Thu, 9 Mar 2017 17:11:54 -0500
"Eric S. Raymond" <esr at thyrsus.com> wrote:

> > > Too often the unsigned type is then used to compute offsets and
> > > the casts get ugly.  
> > 
> > Gack...  I gotta do a quick U-turn on this.  I found some pretty
> > hidden assumptions that I did not expect in the code.  Assumptions
> > that I will document, in the code, for the next poor soul that
> > looks at this.  
> 
> Good.  Keep digging, and learn to be properly terrified.  Somebody
> other than me should grok this (and know the fear).

Fear?  No.  I'm starting to feel the stupid.

Check out this code in libntp/systime.c step_systime().  Lines 393 + 7:

        fp_sys = tspec_stamp_to_lfp(timets);

        /* get the target time as l_fp */
        fp_sys += fp_ofs;

        /* unfold the new system time */
        timets = lfp_stamp_to_tspec(fp_sys, &pivot);

tspec_stamp_to_lfp() is adding JAN_1970 to timets, thus overflowing it.

So we are now doing modulo arithmetic.

Then we add a simple offset.

And the compenstating stupid: lfp_stamp_to_tspec(fp_sys, &pivot) is now
subtracting JAN_1970 and 'pivot' is doing modulo arithmetic to get rid
of the stink of the initial, and compensting, overflows.

So conceptually we have:

timets = ((timets + JAN_1970) + fp_ofs) - JAN_1970

Plus timets &= pivot to deal with the modulo residue.

This could conceptually could be replaced by:

timets += fp_ofs;

This is not rocket science, this is the simple algebra of
common sub-expression elimination

I was gonna attack this top down, but I think if I get rid of a lot
of this junk from bottom up then each change can stand alone.

And you'll be happier that you can audit each step.

BTW: I don't mean to insult anyone, I'm the first to admit I do
something stupid every day.  Often really stupid.  I used to think it
meant I was going senile, but then I caught my kids doing stupid things
every day, and not knowing it.  So the way to path to enlightenment
travels from ignorant to stupid first.

Of course, I'm likely still going senile too...

RGDS
GARY
---------------------------------------------------------------------------
Gary E. Miller Rellim 109 NW Wilmington Ave., Suite E, Bend, OR 97703
	gem at rellim.com  Tel:+1 541 382 8588

	    Veritas liberabit vos. -- Quid est veritas?
    "If you can’t measure it, you can’t improve it." - Lord Kelvin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <https://lists.ntpsec.org/pipermail/devel/attachments/20170309/b9ee3a85/attachment.bin>


More information about the devel mailing list