[Git][NTPsec/ntpsec][master] 2 commits: Boolification.

Eric S. Raymond gitlab at mg.gitlab.com
Fri Nov 4 01:27:47 UTC 2016


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


Commits:
0a60cfc3 by Eric S. Raymond at 2016-11-03T20:47:32-04:00
Boolification.

- - - - -
8246d296 by Eric S. Raymond at 2016-11-03T21:24:56-04:00
Zero-fill the unused part of the Mode 6 response packets.

This wasn't needed when the clients were all in C, for which the first
NUL is a string terminator.  But Python allows NULs in strings, which
means Python mode 6 clients actually see the trailing garbage.

There's another motivation as well.  I'm chasing a bug that inserts
binary junk in textual Mode 6 responses. Zeroing the packet buffer
before each send will help locate this.

- - - - -


1 changed file:

- ntpd/ntp_control.c


Changes:

=====================================
ntpd/ntp_control.c
=====================================
--- a/ntpd/ntp_control.c
+++ b/ntpd/ntp_control.c
@@ -48,7 +48,7 @@ static	void	ctl_error	(uint8_t);
 static	u_short ctlclkstatus	(struct refclockstat *);
 #endif
 static	void	ctl_flushpkt	(uint8_t);
-static	void	ctl_putdata	(const char *, unsigned int, int);
+static	void	ctl_putdata	(const char *, unsigned int, bool);
 static	void	ctl_putstr	(const char *, const char *, size_t);
 static	void	ctl_putdblf	(const char *, int, int, double);
 #define	ctl_putdbl(tag, d)	ctl_putdblf(tag, 1, 3, d)
@@ -945,10 +945,18 @@ ctl_flushpkt(
 	sendlen = dlen + CTL_HEADER_LEN;
 
 	/*
+	 * Zero-fill the unused part of the packet.  This wasn't needed
+	 * when the clients were all in C, for which the first NUL is
+	 * a string terminator.  But Python allows NULs in strings, 
+	 * which means Python mode 6 clients might actually see the trailing
+	 * garbage.
+	 */
+	memset(rpkt.u.data + sendlen, '\0', sizeof(rpkt.u.data) - sendlen);
+	
+	/*
 	 * Pad to a multiple of 32 bits
 	 */
 	while (sendlen & 0x3) {
-		*datapt++ = '\0';
 		sendlen++;
 	}
 
@@ -967,7 +975,6 @@ ctl_flushpkt(
 		 * begin on a 64 bit boundary.
 		 */
 		while (totlen & 7) {
-			*datapt++ = '\0';
 			totlen++;
 		}
 		keyid = htonl(res_keyid);
@@ -1000,7 +1007,7 @@ static void
 ctl_putdata(
 	const char *dp,
 	unsigned int dlen,
-	int bin			/* set to 1 when data is binary */
+	bool bin		/* set to true when data is binary */
 	)
 {
 	int overhead;
@@ -1084,7 +1091,7 @@ ctl_putstr(
 		cp += len;
 		*cp++ = '"';
 	}
-	ctl_putdata(buffer, (u_int)(cp - buffer), 0);
+	ctl_putdata(buffer, (u_int)(cp - buffer), false);
 }
 
 
@@ -1117,7 +1124,7 @@ ctl_putunqstr(
 		memcpy(cp, data, len);
 		cp += len;
 	}
-	ctl_putdata(buffer, (u_int)(cp - buffer), 0);
+	ctl_putdata(buffer, (u_int)(cp - buffer), false);
 }
 
 
@@ -1145,7 +1152,7 @@ ctl_putdblf(
 	snprintf(cp, sizeof(buffer) - (cp - buffer), use_f ? "%.*f" : "%.*g",
 	    precision, d);
 	cp += strlen(cp);
-	ctl_putdata(buffer, (unsigned)(cp - buffer), 0);
+	ctl_putdata(buffer, (unsigned)(cp - buffer), false);
 }
 
 /*
@@ -1170,7 +1177,7 @@ ctl_putuint(
 	NTP_INSIST((cp - buffer) < (int)sizeof(buffer));
 	snprintf(cp, sizeof(buffer) - (cp - buffer), "%lu", uval);
 	cp += strlen(cp);
-	ctl_putdata(buffer, (unsigned)( cp - buffer ), 0);
+	ctl_putdata(buffer, (unsigned)( cp - buffer ), false);
 }
 
 /*
@@ -1203,7 +1210,7 @@ ctl_putfs(
 		 "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
 		 tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min);
 	cp += strlen(cp);
-	ctl_putdata(buffer, (unsigned)( cp - buffer ), 0);
+	ctl_putdata(buffer, (unsigned)( cp - buffer ), false);
 }
 
 
@@ -1230,7 +1237,7 @@ ctl_puthex(
 	NTP_INSIST((cp - buffer) < (int)sizeof(buffer));
 	snprintf(cp, sizeof(buffer) - (cp - buffer), "0x%lx", uval);
 	cp += strlen(cp);
-	ctl_putdata(buffer,(unsigned)( cp - buffer ), 0);
+	ctl_putdata(buffer,(unsigned)( cp - buffer ), false);
 }
 
 
@@ -1256,7 +1263,7 @@ ctl_putint(
 	NTP_INSIST((cp - buffer) < (int)sizeof(buffer));
 	snprintf(cp, sizeof(buffer) - (cp - buffer), "%ld", ival);
 	cp += strlen(cp);
-	ctl_putdata(buffer, (unsigned)( cp - buffer ), 0);
+	ctl_putdata(buffer, (unsigned)( cp - buffer ), false);
 }
 
 
@@ -1283,7 +1290,7 @@ ctl_putts(
 	snprintf(cp, sizeof(buffer) - (cp - buffer), "0x%08x.%08x",
 		 (u_int)ts->l_ui, (u_int)ts->l_uf);
 	cp += strlen(cp);
-	ctl_putdata(buffer, (unsigned)( cp - buffer ), 0);
+	ctl_putdata(buffer, (unsigned)( cp - buffer ), false);
 }
 
 
@@ -1314,7 +1321,7 @@ ctl_putadr(
 	NTP_INSIST((cp - buffer) < (int)sizeof(buffer));
 	snprintf(cp, sizeof(buffer) - (cp - buffer), "%s", cq);
 	cp += strlen(cp);
-	ctl_putdata(buffer, (unsigned)(cp - buffer), 0);
+	ctl_putdata(buffer, (unsigned)(cp - buffer), false);
 }
 
 
@@ -1387,7 +1394,7 @@ ctl_putarray(
 			 " %.2f", arr[i] * 1e3);
 		cp += strlen(cp);
 	} while (i != start);
-	ctl_putdata(buffer, (unsigned)(cp - buffer), 0);
+	ctl_putdata(buffer, (unsigned)(cp - buffer), false);
 }
 
 
@@ -1584,7 +1591,7 @@ ctl_putsys(
 		*buffp++ = '"';
 		*buffp = '\0';
 
-		ctl_putdata(buf, (unsigned)( buffp - buf ), 0);
+		ctl_putdata(buf, (unsigned)( buffp - buf ), false);
 		break;
 	}
 
@@ -2229,7 +2236,7 @@ ctl_putpeer(
 		if (s + 2 < be) {
 			*s++ = '"';
 			*s = '\0';
-			ctl_putdata(buf, (u_int)(s - buf), 0);
+			ctl_putdata(buf, (u_int)(s - buf), false);
 		}
 		break;
 
@@ -2426,7 +2433,7 @@ ctl_putclock(
 
 		*s++ = '"';
 		*s = '\0';
-		ctl_putdata(buf, (unsigned)(s - buf), 0);
+		ctl_putdata(buf, (unsigned)(s - buf), false);
 		break;
 	}
 }
@@ -2611,12 +2618,12 @@ read_status(
 		/* two entries each loop iteration, so n + 1 */
 		if (n + 1 >= COUNTOF(a_st)) {
 			ctl_putdata((void *)a_st, n * sizeof(a_st[0]),
-				    1);
+				    true);
 			n = 0;
 		}
 	}
 	if (n)
-		ctl_putdata((void *)a_st, n * sizeof(a_st[0]), 1);
+		ctl_putdata((void *)a_st, n * sizeof(a_st[0]), true);
 	ctl_flushpkt(0);
 }
 
@@ -2726,7 +2733,7 @@ read_sysvars(void)
 		for (n = 0; n + CS_MAXCODE + 1 < wants_count; n++)
 			if (wants[n + CS_MAXCODE + 1]) {
 				pch = ext_sys_var[n].text;
-				ctl_putdata(pch, strlen(pch), 0);
+				ctl_putdata(pch, strlen(pch), false);
 			}
 	} else {
 		for (cs = def_sys_var; *cs != 0; cs++)
@@ -2734,7 +2741,7 @@ read_sysvars(void)
 		for (kv = ext_sys_var; kv && !(EOV & kv->flags); kv++)
 			if (DEF & kv->flags)
 				ctl_putdata(kv->text, strlen(kv->text),
-					    0);
+					    false);
 	}
 	free(wants);
 	ctl_flushpkt(0);
@@ -2888,7 +2895,7 @@ static void configure(
 			 sizeof(remote_config.err_msg),
 			 "runtime configuration prohibited by restrict ... nomodify");
 		ctl_putdata(remote_config.err_msg,
-			    strlen(remote_config.err_msg), 0);
+			    strlen(remote_config.err_msg), false);
 		ctl_flushpkt(0);
 		NLOG(NLOG_SYSINFO)
 			msyslog(LOG_NOTICE,
@@ -2906,7 +2913,7 @@ static void configure(
 			 sizeof(remote_config.err_msg),
 			 "runtime configuration failed: request too long");
 		ctl_putdata(remote_config.err_msg,
-			    strlen(remote_config.err_msg), 0);
+			    strlen(remote_config.err_msg), false);
 		ctl_flushpkt(0);
 		msyslog(LOG_NOTICE,
 			"runtime config from %s rejected: request too long",
@@ -2956,7 +2963,7 @@ static void configure(
 			remote_config.err_pos += retval;
 	}
 
-	ctl_putdata(remote_config.err_msg, remote_config.err_pos, 0);
+	ctl_putdata(remote_config.err_msg, remote_config.err_pos, false);
 	ctl_flushpkt(0);
 
 	DPRINTF(1, ("Reply: %s\n", remote_config.err_msg));



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/85d98b9776737aae0cd92c6ac7a6dca5f699d944...8246d29698d4df9aadf67afbbd81694bbcffacc7
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161104/07fc1362/attachment.html>


More information about the vc mailing list