Subject: lib/18170: caltontp() function in NTP subsystem ignores "seconds" field of input argument
To: None <>
From: None <>
List: netbsd-bugs
Date: 09/04/2002 13:12:57
>Number:         18170
>Category:       lib
>Synopsis:       caltontp() function in NTP subsystem ignores "seconds" field of input argument
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Sep 04 13:13:00 PDT 2002
>Originator:     Rob Lester
>Release:        1.5
Quantum Corporation
An embedded system which implements NTP support.
The function caltontp() is used to convert a date/time timestamp to
an NTP compliant "number of seconds since 1/1/1900" timestamp.  From
a review of the sources, it is clear that the seconds field 
"second" of the input argument "struct calendar *jt" is not used by

Note however that the function which does conversions in the other
direction - caljulian() - does create the seconds field in the output
calendar structure when necessary.

This means that if you take a calendar structure, convert it to NTP
with caltontp(), and then reverse the conversion with caljulian(),
the resulting calendar structure has a zero in the seconds field
regardless of the seconds field in the input calendar structure.
The result is that up to a 1 minute precision error can occur when
restoring a saved NTP timestamp to calendar format.

1. Instantiate a "struct calendar" variable named jt and an unsigned
   long named ntp.
2. Initialize the fields in the variable jt to anything, except that
   the field "second" must be non-zero.
3. Convert to an NTP timestamp: ntp = caltontp(&jt)
4. Convert back to calendar format: caljulian(ntp, &jt);

Now, jt.second is not the same as when you originally started.  A loss
in precision of up to 1 minute has occurred due to the ignored
seconds field when caltontp() was called.

In caltontp(), change:
 ntp_time = 
    ntp_days*SECSPERDAY+SECSPERMIN*(MINSPERHR*jt->hour + jt->minute);

To read:
 ntp_time = 
    ntp_days*SECSPERDAY+SECSPERMIN*(MINSPERHR*jt->hour + jt->minute)