[Git][NTPsec/ntpsec][master] Eliminate another union.

Eric S. Raymond gitlab at mg.gitlab.com
Sun Jan 8 05:49:18 UTC 2017


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
33a5f601 by Eric S. Raymond at 2017-01-08T00:49:08-05:00
Eliminate another union.

- - - - -


4 changed files:

- include/parse.h
- libparse/clk_rawdcf.c
- libparse/parse.c
- ntpd/refclock_generic.c


Changes:

=====================================
include/parse.h
=====================================
--- a/include/parse.h
+++ b/include/parse.h
@@ -109,11 +109,7 @@
  */
 #define PARSE_TCMAX	    400	  /* maximum addition data size */
 
-typedef union
-{
-  struct timeval tv;		/* timeval - kernel view */
-  l_fp           fp;		/* fixed point - ntp view */
-} timestamp_t;
+typedef l_fp timestamp_t;
 
 /*
  * standard time stamp structure


=====================================
libparse/clk_rawdcf.c
=====================================
--- a/libparse/clk_rawdcf.c
+++ b/libparse/clk_rawdcf.c
@@ -508,9 +508,9 @@ calc_usecdiff(
 	long delta_usec = 0;
 	l_fp delt;
 
-	delt = ref->fp;
-	setlfpsint(delt, lfpsint(delt) + offset);
-	delt -= base->fp;
+	delt = *ref;
+	bumplfpsint(delt, offset);
+	delt -= *base;
 	delta = lfp_stamp_to_tspec(delt, NULL);
 
 	delta_usec = 1000000 * (int32_t)delta.tv_sec + delta.tv_nsec/1000;
@@ -532,7 +532,18 @@ snt_rawdcf(
 	last_tcode_t  *t = (last_tcode_t *)parseio->parse_pdata;
 	long delta_usec = -1;
 
-	if (t != NULL && t->tminute.tv.tv_sec != 0) {
+	/*
+	 * 2017-01-07: Emergency repair. This guard used to test
+	 * t->tminute.tv.tv_sec, but that cannot have been right as
+	 * that tv_sec field was never set anywhere outside the
+	 * in-kernel Sun module (long discarded) while this code was
+	 * reached in userspace.  It is unknown whether replacing that
+	 * test with the analogous one on an l_fp is correct.  This
+	 * problem was discovered when reducing the timestamp_t union
+	 * - this was one of two references to the UNIX timestamp
+	 * member.
+	 */
+	if (t != NULL && lfpuint(t->tminute) != 0) {
 		delta_usec = calc_usecdiff(ptime, &t->tminute, parseio->parse_index - 1);
 		if (delta_usec < 0)
 			delta_usec = -delta_usec;
@@ -546,7 +557,7 @@ snt_rawdcf(
 	{
 		parseio->parse_dtime.parse_stime = *ptime;
 
-		bumplfpuint(parseio->parse_dtime.parse_time.fp, 1);
+		bumplfpuint(parseio->parse_dtime.parse_time, 1);
 
 		parseprintf(DD_RAWDCF,("parse: snt_rawdcf: time stamp synthesized offset %d seconds\n", parseio->parse_index - 1));
 
@@ -585,7 +596,18 @@ inp_rawdcf(
 		if (t != NULL)
 		{
 			/* remember minute start sample time if timeouts occur in minute raster */
-			if (t->timeout.tv.tv_sec != 0)
+			/*
+			 * 2017-01-07: Emergency repair.  This used to test
+			 * t->timeout.tv.tv_sec != 0, but that cannot have been
+			 * right as the tv.tv_sec part of (what used to be) the
+			 * timestamp_t union was only set in the long-discarded
+			 * Sun kernel driver, and this could always be called
+			 * from userspace.  Problem discovered while eliminating
+			 * the timestamp_t union; this was one of only two
+			 * referebces to the timrspec member. It is unknown
+			 * whether this change actually corrects the code.
+			 */
+			if (lfpuint(t->timeout) != 0)
 			{
 				delta_usec = calc_usecdiff(tstamp, &t->timeout, 60);
 				if (delta_usec < 0)


=====================================
libparse/parse.c
=====================================
--- a/libparse/parse.c
+++ b/libparse/parse.c
@@ -35,8 +35,8 @@ parse_timedout(
 
 	l_fp delt;
 
-	delt = tstamp->fp;
-	delt -= parseio->parse_lastchar.fp;
+	delt = *tstamp;
+	delt -= parseio->parse_lastchar;
 	delta = lfp_uintv_to_tspec(delt);
 	if (cmp_tspec(delta, *del) == TIMESPEC_GREATER_THAN)
 	{
@@ -481,7 +481,7 @@ updatetimeinfo(
 
 	parseprintf(DD_PARSE, ("updatetimeinfo status=0x%lx, time=%x\n",
 			       (long)parseio->parse_dtime.parse_state,
-	                       lfpuint(parseio->parse_dtime.parse_time.fp)));
+	                       lfpuint(parseio->parse_dtime.parse_time)));
 
 	return CVT_OK;		/* everything fine and dandy... */
 }
@@ -621,7 +621,7 @@ timepacket(
 	 * time stamp
 	 */
 	struct timespec ts = {t, clock_time.usecond * 1000};
-	parseio->parse_dtime.parse_time.fp = tspec_stamp_to_lfp(ts);
+	parseio->parse_dtime.parse_time = tspec_stamp_to_lfp(ts);
 	
 	parseio->parse_dtime.parse_format       = format;
 


=====================================
ntpd/refclock_generic.c
=====================================
--- a/ntpd/refclock_generic.c
+++ b/ntpd/refclock_generic.c
@@ -1812,7 +1812,7 @@ local_input(
 	 */
 	count = rbufp->recv_length;
 	s = (unsigned char *)rbufp->recv_buffer;
-	ts.fp = rbufp->recv_time;
+	ts = rbufp->recv_time;
 
 	while (count--)
 	{
@@ -1851,18 +1851,18 @@ local_input(
 							else
 							  pts = pps_info.assert_timestamp;
 
-							setlfpuint(parse->parseio.parse_dtime.parse_ptime.fp, (uint32_t) (pts.tv_sec + JAN_1970));
+							setlfpuint(parse->parseio.parse_dtime.parse_ptime, (uint32_t) (pts.tv_sec + JAN_1970));
 
 							dtemp = (double) pts.tv_nsec / 1e9;
 							if (dtemp < 0.) {
 								dtemp += 1;
-								bumplfpuint(parse->parseio.parse_dtime.parse_ptime.fp, -1);
+								bumplfpuint(parse->parseio.parse_dtime.parse_ptime, -1);
 							}
 							if (dtemp > 1.) {
 								dtemp -= 1;
-								bumplfpuint(parse->parseio.parse_dtime.parse_ptime.fp, 1);
+								bumplfpuint(parse->parseio.parse_dtime.parse_ptime, 1);
 							}
-							setlfpfrac(parse->parseio.parse_dtime.parse_ptime.fp, (uint32_t)(dtemp * FRAC));
+							setlfpfrac(parse->parseio.parse_dtime.parse_ptime, (uint32_t)(dtemp * FRAC));
 
 							parse->parseio.parse_dtime.parse_state |= PARSEB_PPS|PARSEB_S_PPS;
 #ifdef DEBUG
@@ -1872,7 +1872,7 @@ local_input(
 								       "parse: local_receive: fd %d PPSAPI seq %ld - PPS %s\n",
 								       rbufp->fd,
 								       (long)pps_info.assert_sequence + (long)pps_info.clear_sequence,
-								       lfptoa(parse->parseio.parse_dtime.parse_ptime.fp, 6));
+								       lfptoa(parse->parseio.parse_dtime.parse_ptime, 6));
 							}
 #endif
 						}
@@ -1975,12 +1975,12 @@ local_receive(
 		   parse->peer->refclkunit,
 		   (unsigned int)parsetime.parse_status,
 		   (unsigned int)parsetime.parse_state,
-		   (unsigned long)lfpuint(parsetime.parse_time.fp),
-		   (unsigned long)lfpfrac(parsetime.parse_time.fp),
-		   (unsigned long)lfpuint(parsetime.parse_stime.fp),
-		   (unsigned long)lfpfrac(parsetime.parse_stime.fp),
-		   (unsigned long)lfpuint(parsetime.parse_ptime.fp),
-		   (unsigned long)lfpfrac(parsetime.parse_ptime.fp));
+		   (unsigned long)lfpuint(parsetime.parse_time),
+		   (unsigned long)lfpfrac(parsetime.parse_time),
+		   (unsigned long)lfpuint(parsetime.parse_stime),
+		   (unsigned long)lfpfrac(parsetime.parse_stime),
+		   (unsigned long)lfpuint(parsetime.parse_ptime),
+		   (unsigned long)lfpfrac(parsetime.parse_ptime));
 	  }
 #endif
 
@@ -3037,8 +3037,8 @@ parse_control(
 				 * we have a PPS and RS232 signal - calculate the skew
 				 * WARNING: assumes on TIMECODE == PULSE (timecode after pulse)
 				 */
-				off = parse->timedata.parse_stime.fp;
-				off -= parse->timedata.parse_ptime.fp; /* true offset */
+				off = parse->timedata.parse_stime;
+				off -= parse->timedata.parse_ptime; /* true offset */
 				tt = add_var(&out->kv_list, 80, RO);
 				snprintf(tt, 80, "refclock_ppsskew=%s", lfptoms(off, 6));
 			}
@@ -3047,20 +3047,20 @@ parse_control(
 		if (PARSE_PPS(parse->timedata.parse_state))
 		{
 			tt = add_var(&out->kv_list, 80, RO|DEF);
-			snprintf(tt, 80, "refclock_ppstime=\"%s\"", gmprettydate(parse->timedata.parse_ptime.fp));
+			snprintf(tt, 80, "refclock_ppstime=\"%s\"", gmprettydate(parse->timedata.parse_ptime));
 		}
 
 		start = tt = add_var(&out->kv_list, 128, RO|DEF);
 		tt = ap(start, 128, tt, "refclock_time=\"");
 
-		if (lfpuint(parse->timedata.parse_time.fp) == 0)
+		if (lfpuint(parse->timedata.parse_time) == 0)
 		{
 			ap(start, 128, tt, "<UNDEFINED>\"");
 		}
 		else
 		{
 			ap(start, 128, tt, "%s\"",
-			    gmprettydate(parse->timedata.parse_time.fp));
+			    gmprettydate(parse->timedata.parse_time));
 		}
 
 		if (!PARSE_GETTIMECODE(parse, &tmpctl))
@@ -3467,8 +3467,8 @@ parse_process(
 
 	if (PARSE_TIMECODE(parsetime->parse_state))
 	{
-		rectime = parsetime->parse_stime.fp;
-		off = reftime = parsetime->parse_time.fp;
+		rectime = parsetime->parse_stime;
+		off = reftime = parsetime->parse_time;
 
 		off -= rectime; /* prepare for PPS adjustments logic */
 
@@ -3500,7 +3500,7 @@ parse_process(
 		/*
 		 * we have a PPS signal - much better than the RS232 stuff (we hope)
 		 */
-		offset = parsetime->parse_ptime.fp;
+		offset = parsetime->parse_ptime;
 
 #ifdef DEBUG
 		if (debug > 3)
@@ -3537,7 +3537,7 @@ parse_process(
 					/*
 					 * time code describes pulse
 					 */
-					reftime = off = parsetime->parse_time.fp;
+					reftime = off = parsetime->parse_time;
 
 					off -= offset; /* true offset */
 				}



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/33a5f6018592bf3f827a2c98cbd4de929b1ec4fc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170108/ba8f3eea/attachment.html>


More information about the vc mailing list