[Git][NTPsec/ntpsec][master] 16 commits: ntp_control: Cleanup ctl_putadr()o

Gary E. Miller gitlab at mg.gitlab.com
Thu Jan 31 02:39:59 UTC 2019


Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
7dab0b32 by Gary E. Miller at 2019-01-31T00:44:14Z
ntp_control: Cleanup ctl_putadr()o

All that pointer arithmetic hard to follow and error prone.  Then
using snprintf() to do a simple copy lost all the speed anyway.

- - - - -
04cdea25 by Gary E. Miller at 2019-01-31T00:53:40Z
ntp_control: Cleanup ctl_putts()

replace a bunch of convoluted pointer stuff.

- - - - -
04341353 by Gary E. Miller at 2019-01-31T01:05:23Z
intp_control: Cleanup ctl_putstr()

Replace a bunch of pointer arithmetic with strlc**().

- - - - -
da99fbee by Gary E. Miller at 2019-01-31T01:13:09Z
ntp_control: Cleanup ctl_putunqstr()

Remove more pointer madness.

- - - - -
d5392adf by Gary E. Miller at 2019-01-31T01:35:51Z
ntp_control: Cleanup ctl_putdblf()

replace pointer arithmetic with strlc**()

- - - - -
dcfc659a by Gary E. Miller at 2019-01-31T01:46:19Z
ntp_control: Cleanup ctl_putuint()

- - - - -
7fed4d1a by Gary E. Miller at 2019-01-31T01:55:18Z
ntp_control: Cleanup ctl_puttime()

- - - - -
40747903 by Gary E. Miller at 2019-01-31T02:00:04Z
ntp_control: Cleanup ctl_puthex()

- - - - -
1d60d22f by Gary E. Miller at 2019-01-31T02:03:57Z
ntp_control: Cleanup ctl_putint()

- - - - -
4cbef07b by Gary E. Miller at 2019-01-31T02:05:12Z
ntp_trontol: Cleanup ctl_putts()

- - - - -
34fddab1 by Gary E. Miller at 2019-01-31T02:16:58Z
ntp_control: Cleanup ctl_putarray()

- - - - -
774549ff by Gary E. Miller at 2019-01-31T02:26:33Z
ntp_control: Cleanup ctl_putsys().  Just whitespace changes.

- - - - -
014bc070 by Gary E. Miller at 2019-01-31T02:30:45Z
ntp_control: Fix overlong line in ctl_getitem(). No functional change.

- - - - -
8b6fd382 by Gary E. Miller at 2019-01-31T02:33:05Z
ntp_control: Fix overlong lines in configure().  No functional change.

- - - - -
38404d18 by Gary E. Miller at 2019-01-31T02:35:06Z
ntp_control: read_mru_list() Fix overlong lines.  No functional change.

- - - - -
8f8ab847 by Gary E. Miller at 2019-01-31T02:38:04Z
ntp_control: Fix overlong line in set_var()

- - - - -


1 changed file:

- ntpd/ntp_control.c


Changes:

=====================================
ntpd/ntp_control.c
=====================================
@@ -1138,9 +1138,6 @@ ctl_putdata(
  *		as in ctl_putstr("var", "value", strlen("value"));
  *		The write will be truncated if data contains  a NUL,
  *		so don't do that.
- *
- * ESR, 2016: Whoever wrote this should be *hurt*.  If the string value is
- * empty, no "=" and no value literal is written, just the bare tag.
  */
 static void
 ctl_putstr(
@@ -1150,14 +1147,14 @@ ctl_putstr(
 	)
 {
 	char buffer[512];
-	size_t tl = strlen(tag);
 
-	if (tl >= sizeof(buffer))
-	    return;
-	memcpy(buffer, tag, tl);
-	if (len > 0)
-	    snprintf(buffer + tl, sizeof(buffer) - tl, "=\"%s\"", data);
-	ctl_putdata(buffer, (unsigned int)strlen(buffer), false);
+        strlcpy(buffer, tag, sizeof(buffer));
+        strlcat(buffer, "=\"", sizeof(buffer));
+        if (0 < len)
+            strlcat(buffer, data, sizeof(buffer));
+        strlcat(buffer, "\"", sizeof(buffer));
+
+	ctl_putdata(buffer, strlen(buffer), false);
 }
 
 
@@ -1178,20 +1175,15 @@ ctl_putunqstr(
 	)
 {
 	char buffer[512];
-	char *cp;
-	size_t tl;
 
-	tl = strlen(tag);
-	if (tl + 1 + len >= sizeof(buffer))
+	if ((strlen(tag) + 2 + len) >= sizeof(buffer))
 	    return;
-	memcpy(buffer, tag, tl);
-	cp = buffer + tl;
-	if (len > 0) {
-		*cp++ = '=';
-		memcpy(cp, data, len);
-		cp += len;
-	}
-	ctl_putdata(buffer, (unsigned int)(cp - buffer), false);
+
+        strlcpy(buffer, tag, sizeof(buffer));
+        strlcat(buffer, "=", sizeof(buffer));
+	if (len > 0)
+            strlcat(buffer, data, sizeof(buffer));
+	ctl_putdata(buffer, strlen(buffer), false);
 }
 
 
@@ -1206,21 +1198,14 @@ ctl_putdblf(
 	double		d
 	)
 {
-	char *cp;
-	const char *cq;
-	char buffer[200];
+        char buffer[200];
+        char buf[50];
 
-	cp = buffer;
-	cq = tag;
-	while (*cq != '\0' && cp < buffer + sizeof(buffer) - 1)
-		*cp++ = *cq++;
-	*cp++ = '=';
-	INSIST((size_t)(cp - buffer) < sizeof(buffer));
-	snprintf(cp, sizeof(buffer) - (size_t)(cp - buffer),
-                 use_f ? "%.*f" : "%.*g",
-	    precision, d);
-	cp += strlen(cp);
-	ctl_putdata(buffer, (unsigned)(cp - buffer), false);
+        strlcpy(buffer, tag, sizeof(buffer));
+        snprintf(buf, sizeof(buf), use_f ? "=%.*f" : "%.*g", precision, d);
+        strlcat(buffer, buf, sizeof(buffer));
+
+        ctl_putdata(buffer, strlen(buffer), false);
 }
 
 /*
@@ -1232,20 +1217,15 @@ ctl_putuint(
 	uint64_t uval
 	)
 {
-	char *cp;
-	const char *cq;
-	char buffer[200];
+        char buffer[200];
+        char buf[50];
+
+        strlcpy(buffer, tag, sizeof(buffer));
 
-	cp = buffer;
-	cq = tag;
-	while (*cq != '\0' && cp < buffer + sizeof(buffer) - 1)
-		*cp++ = *cq++;
+        snprintf(buf, sizeof(buf), "=%" PRIu64, uval);
+        strlcat(buffer, buf, sizeof(buffer));
 
-	*cp++ = '=';
-	INSIST((cp - buffer) < (int)sizeof(buffer));
-	snprintf(cp, sizeof(buffer) - (size_t)(cp - buffer), "%" PRIu64, uval);
-	cp += strlen(cp);
-	ctl_putdata(buffer, (unsigned)( cp - buffer ), false);
+        ctl_putdata(buffer, strlen(buffer), false);
 }
 
 /*
@@ -1257,27 +1237,21 @@ ctl_puttime(
 	time_t uval
 	)
 {
-	char *cp;
-	const char *cq;
 	char buffer[200];
 	struct tm tmbuf, *tm = NULL;
-	time_t fstamp = uval;
+        char buf[50];
 
-	cp = buffer;
-	cq = tag;
-	while (*cq != '\0' && cp < buffer + sizeof(buffer) - 1)
-		*cp++ = *cq++;
+        strlcpy(buffer, tag, sizeof(buffer));
 
-	*cp++ = '=';
-	tm = gmtime_r(&fstamp, &tmbuf);
+	tm = gmtime_r(&uval, &tmbuf);
 	if (NULL == tm)
 		return;
-	INSIST((cp - buffer) < (int)sizeof(buffer));
-	snprintf(cp, sizeof(buffer) - (size_t)(cp - buffer),
-		 "%04d-%02d-%02dT%02d:%02dZ", tm->tm_year + 1900,
+	snprintf(buf, sizeof(buf), "=%04d-%02d-%02dT%02d:%02dZ",
+                 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 ), false);
+        strlcat(buffer, buf, sizeof(buffer));
+
+	ctl_putdata(buffer, strlen(buf), false);
 }
 
 
@@ -1291,20 +1265,15 @@ ctl_puthex(
 	uint64_t uval
 	)
 {
-	char *cp;
-	const char *cq;
 	char buffer[200];
+        char buf[50];
+
+        strlcpy(buffer, tag, sizeof(buffer));
 
-	cp = buffer;
-	cq = tag;
-	while (*cq != '\0' && cp < buffer + sizeof(buffer) - 1)
-		*cp++ = *cq++;
+	snprintf(buf, sizeof(buf), "=0x%" PRIx64, uval);
+        strlcat(buffer, buf, sizeof(buffer));
 
-	*cp++ = '=';
-	INSIST((cp - buffer) < (int)sizeof(buffer));
-	snprintf(cp, sizeof(buffer) - (size_t)(cp - buffer), "0x%" PRIx64, uval);
-	cp += strlen(cp);
-	ctl_putdata(buffer,(unsigned)( cp - buffer ), false);
+	ctl_putdata(buffer, strlen(buffer), false);
 }
 
 
@@ -1317,20 +1286,15 @@ ctl_putint(
 	long ival
 	)
 {
-	char *cp;
-	const char *cq;
 	char buffer[200];
+        char buf[50];
+
+        strlcpy(buffer, tag, sizeof(buffer));
 
-	cp = buffer;
-	cq = tag;
-	while (*cq != '\0' && cp < buffer + sizeof(buffer) - 1)
-		*cp++ = *cq++;
+	snprintf(buf, sizeof(buf), "=%ld", ival);
+        strlcat(buffer, buf, sizeof(buffer));
 
-	*cp++ = '=';
-	INSIST((cp - buffer) < (int)sizeof(buffer));
-	snprintf(cp, sizeof(buffer) - (size_t)(cp - buffer), "%ld", ival);
-	cp += strlen(cp);
-	ctl_putdata(buffer, (unsigned)( cp - buffer ), false);
+	ctl_putdata(buffer, strlen(buffer), false);
 }
 
 
@@ -1343,21 +1307,15 @@ ctl_putts(
 	l_fp *ts
 	)
 {
-	char *cp;
-	const char *cq;
 	char buffer[200];
+	char buf[50];
 
-	cp = buffer;
-	cq = tag;
-	while (*cq != '\0' && cp < buffer + sizeof(buffer) - 1)
-		*cp++ = *cq++;
+        strlcpy(buffer, tag, sizeof(buffer));
 
-	*cp++ = '=';
-	INSIST((size_t)(cp - buffer) < sizeof(buffer));
-	snprintf(cp, sizeof(buffer) - (size_t)(cp - buffer), "0x%08x.%08x",
+	snprintf(buf, sizeof(buf), "=0x%08x.%08x",
 		 (unsigned int)lfpuint(*ts), (unsigned int)lfpfrac(*ts));
-	cp += strlen(cp);
-	ctl_putdata(buffer, (unsigned)( cp - buffer ), false);
+        strlcat(buffer, buf, sizeof(buffer));
+	ctl_putdata(buffer, strlen(buffer), false);
 }
 
 
@@ -1371,27 +1329,21 @@ ctl_putadr(
 	sockaddr_u *addr
 	)
 {
-	char *cp;
-	const char *cq;
+        const char *cq;
 	char buffer[200];
 
-	cp = buffer;
-	cq = tag;
-	while (*cq != '\0' && cp < buffer + sizeof(buffer) - 1)
-		*cp++ = *cq++;
+        strlcpy(buffer, tag, sizeof(buffer));
+        strlcat(buffer, "=", sizeof(buffer));
 
-	*cp++ = '=';
 	if (NULL == addr) {
 		struct in_addr in4;
 		in4.s_addr = addr32;
 		cq = inet_ntoa(in4);
-	}
-	else
+	} else
 		cq = socktoa(addr);
-	INSIST((cp - buffer) < (int)sizeof(buffer));
-	snprintf(cp, sizeof(buffer) - (size_t)(cp - buffer), "%s", cq);
-	cp += strlen(cp);
-	ctl_putdata(buffer, (unsigned)(cp - buffer), false);
+
+        strlcat(buffer, cq, sizeof(buffer));
+	ctl_putdata(buffer, strlen(buffer), false);
 }
 
 
@@ -1437,26 +1389,23 @@ ctl_putarray(
 	int start
 	)
 {
-	char *cp;
-	const char *cq;
 	char buffer[200];
+	char buf[50];
 	int i;
-	cp = buffer;
-	cq = tag;
-	while (*cq != '\0' && cp < buffer + sizeof(buffer) - 1)
-		*cp++ = *cq++;
-	*cp++ = '=';
+
+        strlcpy(buffer, tag, sizeof(buffer));
+        strlcat(buffer, "=", sizeof(buffer));
+
 	i = start;
 	do {
 		if (i == 0)
 			i = NTP_SHIFT;
 		i--;
-		INSIST((cp - buffer) < (int)sizeof(buffer));
-		snprintf(cp, sizeof(buffer) - (size_t)(cp - buffer),
-			 " %.2f", arr[i] * MS_PER_S);
-		cp += strlen(cp);
+		snprintf(buf, sizeof(buf), " %.2f", arr[i] * MS_PER_S);
+                strlcat(buffer, buf, sizeof(buffer));
 	} while (i != start);
-	ctl_putdata(buffer, (unsigned)(cp - buffer), false);
+
+	ctl_putdata(buffer, strlen(buffer), false);
 }
 
 
@@ -1482,9 +1431,10 @@ ctl_putsys(
 	    current_time != ntp_adjtime_time) {
 		ZERO(ntx);
 		if (ntp_adjtime(&ntx) < 0)
-			msyslog(LOG_ERR, "MODE6: ntp_adjtime() for mode 6 query failed: %m");
+                    msyslog(LOG_ERR,
+                            "MODE6: ntp_adjtime() for mode 6 query failed: %m");
 		else
-			ntp_adjtime_time = current_time;
+                    ntp_adjtime_time = current_time;
 	}
 
 	switch (varid) {
@@ -1502,7 +1452,8 @@ ctl_putsys(
 		break;
 
 	case CS_ROOTDELAY:
-		ctl_putdbl(sys_var[CS_ROOTDELAY].text, sys_vars.sys_rootdelay * MS_PER_S);
+		ctl_putdbl(sys_var[CS_ROOTDELAY].text,
+                           sys_vars.sys_rootdelay * MS_PER_S);
 		break;
 
 	case CS_ROOTDISPERSION:
@@ -1511,8 +1462,10 @@ ctl_putsys(
 		break;
 
 	case CS_REFID:
-		if (sys_vars.sys_stratum > 1 && sys_vars.sys_stratum < STRATUM_UNSPEC)
-			ctl_putadr(sys_var[varid].text, sys_vars.sys_refid, NULL);
+		if (sys_vars.sys_stratum > 1 &&
+                    sys_vars.sys_stratum < STRATUM_UNSPEC)
+			ctl_putadr(sys_var[varid].text, sys_vars.sys_refid,
+                                   NULL);
 		else
 			ctl_putrefid(sys_var[varid].text, sys_vars.sys_refid);
 		break;
@@ -1534,7 +1487,8 @@ ctl_putsys(
 		break;
 
 	case CS_PEERADR:
-		if (sys_vars.sys_peer != NULL && sys_vars.sys_peer->dstadr != NULL)
+		if (sys_vars.sys_peer != NULL &&
+                    sys_vars.sys_peer->dstadr != NULL)
 			ss = sockporttoa(&sys_vars.sys_peer->srcadr);
 		else
 			ss = "0.0.0.0:0";
@@ -1591,7 +1545,8 @@ ctl_putsys(
 
 	case CS_STABIL:
 		/* a.k.a clk_wander (s/s), output as us/s */
-		ctl_putdbl6(sys_var[CS_STABIL].text, clock_stability * US_PER_S);
+		ctl_putdbl6(sys_var[CS_STABIL].text,
+                            clock_stability * US_PER_S);
 		break;
 
 	case CS_VARLIST:
@@ -1681,7 +1636,8 @@ ctl_putsys(
 #ifdef ENABLE_LEAP_SMEAR
 	case CS_LEAPSMEARINTV:
 		if (leap_smear_intv > 0)
-			ctl_putuint(sys_var[CS_LEAPSMEARINTV].text, leap_smear_intv);
+			ctl_putuint(sys_var[CS_LEAPSMEARINTV].text,
+                                    leap_smear_intv);
 		break;
 
 	case CS_LEAPSMEAROFFS:
@@ -1706,7 +1662,7 @@ ctl_putsys(
 	case CS_MRU_MEM: {
 		uint64_t u;
 		u = mon_data.mru_entries * sizeof(mon_entry);
-		u = (u+512)/1024;
+		u = (u + 512) / 1024;
 		ctl_putuint(sys_var[varid].text, u);
 		break;
 		}
@@ -1734,7 +1690,7 @@ ctl_putsys(
 	case CS_MRU_MAXMEM: {
 		uint64_t u;
 		u = mon_data.mru_maxdepth * sizeof(mon_entry);
-		u = (u+512)/1024;
+		u = (u + 512) / 1024;
 		ctl_putuint(sys_var[varid].text, u);
 		break;
 		}
@@ -1772,7 +1728,7 @@ ctl_putsys(
 
 	case CS_SS_RESET:
 		ctl_putuint(sys_var[varid].text,
-					current_time - stat_count.sys_stattime);
+			    current_time - stat_count.sys_stattime);
 		break;
 
 	case CS_SS_RECEIVED:
@@ -1825,7 +1781,8 @@ ctl_putsys(
 		break;
 
 	case CS_AUTHFREEK:
-		ctl_putuint(sys_var[varid].text, (unsigned long)authnumfreekeys);
+		ctl_putuint(sys_var[varid].text,
+                            (unsigned long)authnumfreekeys);
 		break;
 
 	case CS_AUTHKLOOKUPS:
@@ -1917,7 +1874,7 @@ ctl_putsys(
 		break;
 
 	case CS_K_FREQTOL:
-	    ctl_putsfp(sys_var[varid].text, ntx.tolerance);
+	        ctl_putsfp(sys_var[varid].text, ntx.tolerance);
 		break;
 
 	case CS_K_PPS_FREQ:
@@ -2641,7 +2598,8 @@ ctl_getitem(
 	 * http://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=53618
 	 */
 		    msyslog(LOG_WARNING,
-			    "Possible 'ntpdx' exploit from %s#%" PRIu16 " (possibly spoofed)",
+			    "Possible 'ntpdx' exploit from %s#%" PRIu16
+                            " (possibly spoofed)",
 			    socktoa(rmt_addr), (unsigned)SRCPORT(rmt_addr));
 	    }
 	reqpt = reqend; /* never again for this packet! */
@@ -3012,13 +2970,15 @@ static void configure(
 	if (RES_NOMODIFY & restrict_mask) {
 		snprintf(remote_config.err_msg,
 			 sizeof(remote_config.err_msg),
-			 "runtime configuration prohibited by restrict ... nomodify");
+			 "runtime configuration prohibited by restrict ..."
+                         " nomodify");
 		ctl_putdata(remote_config.err_msg,
 			    strlen(remote_config.err_msg), false);
 		ctl_flushpkt(0);
 		NLOG(NLOG_SYSINFO)
 			msyslog(LOG_NOTICE,
-				"MODE6: runtime config from %s rejected due to nomodify restriction",
+				"MODE6: runtime config from %s rejected due"
+                                " to nomodify restriction",
 				socktoa(&rbufp->recv_srcadr));
 		stat_count.sys_restricted++;
 		return;
@@ -3035,7 +2995,8 @@ static void configure(
 			    strlen(remote_config.err_msg), false);
 		ctl_flushpkt(0);
 		msyslog(LOG_NOTICE,
-			"MODE6: runtime config from %s rejected: request too long",
+			"MODE6: runtime config from %s rejected: request"
+                        " too long",
 			socktoa(&rbufp->recv_srcadr));
 		return;
 	}
@@ -3471,7 +3432,8 @@ static void read_mru_list(
 		ctl_error(CERR_PERMISSION);
 		NLOG(NLOG_SYSINFO)
 			msyslog(LOG_NOTICE,
-				"MODE6: mrulist from %s rejected due to nomrulist restriction",
+				"MODE6: mrulist from %s rejected due to"
+                                " nomrulist restriction",
 				socktoa(&rbufp->recv_srcadr));
 		stat_count.sys_restricted++;
 		return;
@@ -3564,12 +3526,14 @@ static void read_mru_list(
 			if (lfpuint(last[si]) && lfpfrac(last[si]) && si == priors)
 				priors++;
 		} else {
-			DPRINT(1, ("read_mru_list: invalid key item: '%s' (ignored)\n",
+			DPRINT(1, ("read_mru_list: invalid key item: '%s'"
+                                   " (ignored)\n",
 				   v->text));
 			continue;
 
 		blooper:
-			DPRINT(1, ("read_mru_list: invalid param for '%s': '%s' (bailing)\n",
+			DPRINT(1, ("read_mru_list: invalid param for '%s'"
+                                   ": '%s' (bailing)\n",
 				   v->text, val));
 			free(pnonce);
 			pnonce = NULL;
@@ -4412,11 +4376,12 @@ set_var(
 					t++;
 				}
 				if (*s == *t && ((*t == '=') || !*t)) {
-					td = erealloc((void *)(intptr_t)k->text, size);
-					memcpy(td, data, size);
-					k->text = td;
-					k->flags = def;
-					return;
+                                    td = erealloc((void *)(intptr_t)k->text,
+                                                  size);
+                                    memcpy(td, data, size);
+                                    k->text = td;
+                                    k->flags = def;
+                                    return;
 				}
 			}
 			k++;



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/efd64e03feddf568eabc4143549aaa3945b52943...8f8ab8477b7d192ca6f8fad233dc1cbf2b09aaf2

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/efd64e03feddf568eabc4143549aaa3945b52943...8f8ab8477b7d192ca6f8fad233dc1cbf2b09aaf2
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20190131/dfe6d980/attachment-0001.html>


More information about the vc mailing list