[Git][NTPsec/ntpsec][master] C99 allows for loops to declare counters inline

Matt Selsky gitlab at mg.gitlab.com
Mon Feb 4 14:11:38 UTC 2019


Matt Selsky pushed to branch master at NTPsec / ntpsec


Commits:
b5dfa19b by Matt Selsky at 2019-02-04T14:07:27Z
C99 allows for loops to declare counters inline

- - - - -


15 changed files:

- attic/clocks.c
- attic/digest-timing.c
- libntp/assert.c
- libntp/authreadkeys.c
- ntpd/ntp_io.c
- ntpd/ntp_refclock.c
- ntpd/ntp_restrict.c
- ntpd/ntp_sandbox.c
- ntpd/ntp_scanner.c
- ntpd/refclock_oncore.c
- ntpfrob/dump.c
- ntpfrob/jitter.c
- tests/libntp/macencrypt.c
- tests/libntp/ntp_random.c
- tests/libntp/timespecops.c


Changes:

=====================================
attic/clocks.c
=====================================
@@ -53,7 +53,7 @@ int do_res(int type, const char* name) {
 }
 
 int do_average(int type, const char* name) {
-  int err, i;
+  int err;
   struct timespec start, stop;
   uint64_t sec, nanos;
 
@@ -64,7 +64,7 @@ int do_average(int type, const char* name) {
   }
 
   clock_gettime(CLOCK_REALTIME, &start);
-  for (i = 0; i < BATCHSIZE; i++) {
+  for (int i = 0; i < BATCHSIZE; i++) {
     clock_gettime(type, &stop);
   }
   clock_gettime(CLOCK_REALTIME, &stop);
@@ -77,7 +77,6 @@ int do_average(int type, const char* name) {
 }
 
 int do_fastest(int type, const char* name) {
-  int i;
   struct timespec start, stop;
   uint64_t sec, nanos, fastest;
 
@@ -85,7 +84,7 @@ int do_fastest(int type, const char* name) {
 
   dups = 0;
   fastest = 999999999;
-  for (i = 0; i < BATCHSIZE; i++) {
+  for (int i = 0; i < BATCHSIZE; i++) {
     clock_gettime(type, &start);  /* warm up caches */
     clock_gettime(type, &start);
     clock_gettime(type, &stop);
@@ -162,14 +161,13 @@ void do_hist(int type, int fastest) {
 
 int main(int argc, char *argv[])
 {
-  int i;
   int res, average, fastest;
 
   (void)argc;  /* Squash unused warnings */
   (void)argv;
 
 printf("      res   avg     min  dups  CLOCK\n");
-for (i=0; (NULL != clocks[i].name); i++) {
+for (int i=0; (NULL != clocks[i].name); i++) {
   res = do_res(clocks[i].type, clocks[i].name);
   average = do_average(clocks[i].type, clocks[i].name);
   fastest = do_fastest(clocks[i].type, clocks[i].name);


=====================================
attic/digest-timing.c
=====================================
@@ -140,14 +140,13 @@ static void DoDigest(
   int type = OBJ_sn2nid(name);
   const EVP_MD *digest = EVP_get_digestbynid(type);
   struct timespec start, stop;
-  int i;
   double fast, slow;
   unsigned int digestlength = 0;
 
   if (NULL == digest) return;
 
   clock_gettime(CLOCK_MONOTONIC, &start);
-  for (i = 0; i < NUM; i++) {
+  for (int i = 0; i < NUM; i++) {
     digestlength = SSL_Digest(digest, key, keylength, pkt, pktlength);
   }
   clock_gettime(CLOCK_MONOTONIC, &stop);
@@ -157,7 +156,7 @@ static void DoDigest(
 
 #ifdef DoSLOW
   clock_gettime(CLOCK_MONOTONIC, &start);
-  for (i = 0; i < NUM; i++) {
+  for (int i = 0; i < NUM; i++) {
     digestlength = SSL_DigestSlow(type, key, keylength, pkt, pktlength);
   }
   clock_gettime(CLOCK_MONOTONIC, &stop);
@@ -179,14 +178,13 @@ static void DoCMAC(
 )
 {
   struct timespec start, stop;
-  int i;
   double fast;
   unsigned long digestlength = 0;
 
   if (NULL == cipher) return;
 
   clock_gettime(CLOCK_MONOTONIC, &start);
-  for (i = 0; i < NUM; i++) {
+  for (int i = 0; i < NUM; i++) {
     digestlength = SSL_CMAC(cipher, key, keylength, pkt, pktlength);
   }
   clock_gettime(CLOCK_MONOTONIC, &stop);


=====================================
libntp/assert.c
=====================================
@@ -88,7 +88,6 @@ void
 backtrace_log(void) {
 	trace_arg_t arg;
 	void *buffer[BACKTRACE_MAXFRAME];
-	int i;
 
 	arg.skip_count = 1;
 	arg.result = buffer;
@@ -98,7 +97,7 @@ backtrace_log(void) {
 
 	msyslog(LOG_ERR, "ERR: Stack trace:\n");
 	/* skip trace of this shim function */
-	for (i = 1; i < arg.count; i++) {
+	for (int i = 1; i < arg.count; i++) {
 	    msyslog(LOG_ERR, "ERR: #%d %p in ??\n", i, buffer[i]);
 	}
 


=====================================
libntp/authreadkeys.c
=====================================
@@ -176,7 +176,6 @@ check_cmac_key_length(
 	int keylength) {
     const EVP_CIPHER *cmac_cipher = EVP_get_cipherbyname(name);
     int len = EVP_CIPHER_key_length(cmac_cipher);
-    int i;
 
     if (len < keylength) {
 	    msyslog(LOG_ERR, "AUTH: CMAC key %u will be truncated %d=>%d",
@@ -184,7 +183,7 @@ check_cmac_key_length(
     } else if ( len > keylength) {
 	    msyslog(LOG_ERR, "AUTH: CMAC key %u will be padded %d=>%d",
 		keyno, keylength, len);
-	    for (i=keylength; i<len; i++) key[i] = 0;
+	    for (int i=keylength; i<len; i++) key[i] = 0;
     } else {
 	    if (0) msyslog(LOG_ERR, "AUTH: CMAC key %u is right size", keyno);
     }
@@ -231,7 +230,6 @@ authreadkeys(
 	char *	name;
 	char	namebuf[NAMEBUFSIZE];
 	size_t	len;
-	size_t	j;
 	int	keys = 0;
 
 	/*
@@ -362,6 +360,7 @@ msyslog(LOG_ERR, "AUTH: authreadkeys: reading %s", file);
 			    keyno, (unsigned int)jlim);
 
 			}
+			size_t j;
 			for (j = 0; j < jlim; j++) {
 				char *ptr = strchr(hex, tolower((unsigned char)token[j]));
 				if (ptr == NULL)


=====================================
ntpd/ntp_io.c
=====================================
@@ -269,8 +269,6 @@ maintain_activefds(
 	bool closing
 	)
 {
-	int i;
-
 	if (fd < 0 || fd >= (int)FD_SETSIZE) {
 		msyslog(LOG_ERR,
 			"IO: Too many sockets in use, FD_SETSIZE %d exceeded by fd %d",
@@ -284,7 +282,7 @@ maintain_activefds(
 	} else {
 		FD_CLR(fd, &activefds);
 		if (maxactivefd && fd == maxactivefd) {
-			for (i = maxactivefd - 1; i >= 0; i--)
+			for (int i = maxactivefd - 1; i >= 0; i--)
 				if (FD_ISSET(i, &activefds)) {
 					maxactivefd = i;
 					break;
@@ -2636,7 +2634,6 @@ calc_addr_distance(
 	uint32_t	v4dist;
 	bool	found_greater;
 	bool	a1_greater;
-	int	i;
 
 	REQUIRE(AF(a1) == AF(a2));
 
@@ -2657,7 +2654,7 @@ calc_addr_distance(
 
 	found_greater = false;
 	a1_greater = false;	/* suppress pot. uninit. warning */
-	for (i = 0; i < (int)sizeof(NSRCADR6(a1)); i++) {
+	for (int i = 0; i < (int)sizeof(NSRCADR6(a1)); i++) {
 		if (!found_greater &&
 		    NSRCADR6(a1)[i] != NSRCADR6(a2)[i]) {
 			found_greater = true;
@@ -2687,8 +2684,6 @@ cmp_addr_distance(
 	const sockaddr_u *	d2
 	)
 {
-	int	i;
-
 	REQUIRE(AF(d1) == AF(d2));
 
 	if (IS_IPV4(d1)) {
@@ -2700,7 +2695,7 @@ cmp_addr_distance(
 			return COMPARE_GREATERTHAN;
 	}
 
-	for (i = 0; i < (int)sizeof(NSRCADR6(d1)); i++) {
+	for (int i = 0; i < (int)sizeof(NSRCADR6(d1)); i++) {
 		if (NSRCADR6(d1)[i] < NSRCADR6(d2)[i])
 			return COMPARE_LESSTHAN;
 		else if (NSRCADR6(d1)[i] > NSRCADR6(d2)[i])


=====================================
ntpd/ntp_refclock.c
=====================================
@@ -149,9 +149,7 @@ refclock_name(
 void
 init_refclock(void)
 {
-	int i;
-
-	for (i = 0; i < (int)num_refclock_conf; i++)
+	for (int i = 0; i < (int)num_refclock_conf; i++)
 		if (refclock_conf[i]->clock_init)
 			(refclock_conf[i]->clock_init)();
 }
@@ -731,7 +729,6 @@ refclock_setup(
 	unsigned int	lflags		/* line discipline flags */
 	)
 {
-	int	i;
 	TTY	ttyb, *ttyp;
 
 	/*
@@ -769,7 +766,7 @@ refclock_setup(
 		}
 		cfsetispeed(&ttyb, speed);
 		cfsetospeed(&ttyb, speed);
-		for (i = 0; i < NCCS; ++i)
+		for (int i = 0; i < NCCS; ++i)
 			ttyp->c_cc[i] = '\0';
 
 #ifdef TIOCMGET


=====================================
ntpd/ntp_restrict.c
=====================================
@@ -158,7 +158,6 @@ alloc_res4(void)
 	const size_t	count = INC_RESLIST4;
 	restrict_u *	rl;
 	restrict_u *	res;
-	int		i;
 
 	UNLINK_HEAD_SLIST(res, resfree4, link);
 	if (res != NULL)
@@ -167,7 +166,7 @@ alloc_res4(void)
 	rl = emalloc_zero(count * cb);
 	/* link all but the first onto free list */
 	res = (void *)((char *)rl + (count - 1) * cb);
-	for (i = count - 1; i > 0; i--) {
+	for (int i = count - 1; i > 0; i--) {
 		LINK_SLIST(resfree4, res, link);
 		res = (void *)((char *)res - cb);
 	}
@@ -184,7 +183,6 @@ alloc_res6(void)
 	const size_t	count = INC_RESLIST6;
 	restrict_u *	rl;
 	restrict_u *	res;
-	int		i;
 
 	UNLINK_HEAD_SLIST(res, resfree6, link);
 	if (res != NULL)
@@ -193,7 +191,7 @@ alloc_res6(void)
 	rl = emalloc_zero(count * cb);
 	/* link all but the first onto free list */
 	res = (void *)((char *)rl + (count - 1) * cb);
-	for (i = count - 1; i > 0; i--) {
+	for (int i = count - 1; i > 0; i--) {
 		LINK_SLIST(resfree6, res, link);
 		res = (void *)((char *)res - cb);
 	}


=====================================
ntpd/ntp_sandbox.c
=====================================
@@ -427,9 +427,7 @@ int scmp_sc[] = {
 #endif
 };
 	{
-		unsigned int i;
-
-		for (i = 0; i < COUNTOF(scmp_sc); i++) {
+		for (unsigned int i = 0; i < COUNTOF(scmp_sc); i++) {
 			if (seccomp_rule_add(ctx,
 			    SCMP_ACT_ALLOW, scmp_sc[i], 0) < 0) {
 				msyslog(LOG_ERR,


=====================================
ntpd/ntp_scanner.c
=====================================
@@ -529,12 +529,11 @@ is_keyword(
 	follby fb;
 	int curr_s;		/* current state index */
 	int token;
-	int i;
 
 	curr_s = SCANNER_INIT_S;
 	token = 0;
 
-	for (i = 0; lexeme[i]; i++) {
+	for (int i = 0; lexeme[i]; i++) {
 		while (curr_s && (lexeme[i] != SS_CH(sst[curr_s])))
 			curr_s = (int)SS_OTHER_N(sst[curr_s]);
 


=====================================
ntpd/refclock_oncore.c
=====================================
@@ -3690,15 +3690,15 @@ oncore_print_array(
 	int	n
 	)
 {
-	int	jj, i, j, nn;
+	int	jj, nn;
 
 	nn = 0;
 	printf("\nTOP\n");
 	jj = n/16;
-	for (j=0; j<jj; j++) {
+	for (int j=0; j<jj; j++) {
 		printf("%4d: ", nn);
 		nn += 16;
-		for (i=0; i<16; i++)
+		for (int i=0; i<16; i++)
 			printf(" %o", *cp++);
 		printf("\n");
 	}


=====================================
ntpfrob/dump.c
=====================================
@@ -44,7 +44,7 @@ do_dump(const iomode mode, const int force)
 {
     struct timex txc;
     long adjval[18];
-    int out, i, v, end = 10;
+    int out, v, end = 10;
 
     txc.modes = 0;
     out       = adjtimex(&txc);
@@ -102,14 +102,14 @@ do_dump(const iomode mode, const int force)
     if (mode == json) {
         printf("{\"time\":%ld.%09ld, \"verbose\": \"%s\"", txc.time.tv_sec,
           txc.time.tv_usec, verbose[v]);
-        for (i = 0; i < end; i++) {
+        for (int i = 0; i < end; i++) {
             printf(", \"%s\": %ld", adjkey[i], adjval[i]);
         }
         puts("}\n");
     } else {
         printf("time = %ld.%09ld\n", txc.time.tv_sec, txc.time.tv_usec);
         printf("verbose = \"%s\"\n", verbose[v]);
-        for (i = 0; i < end; i++) {
+        for (int i = 0; i < end; i++) {
             printf("%s = %ld\n", adjkey[i], adjval[i]);
         }
     }


=====================================
ntpfrob/jitter.c
=====================================
@@ -73,20 +73,19 @@ static int doublecmp(const void * a, const void * b)
 void jitter(const iomode mode)
 {
 	l_fp tr;
-	int i;
 	double gtod[NBUF];
 	doubletime_t jitterx;	/* shadow warning on some old systems */
 
 	/*
 	 * Force pages into memory
 	 */
-	for (i = 0; i < NBUF; i ++)
+	for (int i = 0; i < NBUF; i ++)
 	    gtod[i] = 0;
 
 	/*
 	 * Construct gtod array
 	 */
-	for (i = 0; i < NBUF; i ++) {
+	for (int i = 0; i < NBUF; i ++) {
 		get_clocktime(&tr);
 		gtod[i] = lfptod(tr);
 	}
@@ -95,7 +94,7 @@ void jitter(const iomode mode)
 	 * Write out gtod array for later processing with Matlab
 	 */
 	average = 0;
-	for (i = 0; i < (NBUF - 1); i++) {
+	for (int i = 0; i < (NBUF - 1); i++) {
 		gtod[i] = gtod[i + 1] - gtod[i];
 		if (mode == raw)
 			printf("%13.9f\n", gtod[i]);
@@ -110,7 +109,7 @@ void jitter(const iomode mode)
 	// calculate 'variance' and call it jitter
 	// and scale everything up a million time for clarity
 	jitterx = 0;
-	for (i = 0; i < (NBUF - 1); i ++) {
+	for (int i = 0; i < (NBUF - 1); i ++) {
 		gtod[i] = (gtod[i] - average) * 1000000;
 		jitterx += gtod[i] * gtod[i];
 	}
@@ -123,13 +122,13 @@ void jitter(const iomode mode)
 
 	if (mode == json) {
 		fprintf(stdout, "{\"Mean\": %.9Lf, \"High\": [", average);
-		for (i = 0; i < NSAMPLES; i++) {
+		for (int i = 0; i < NSAMPLES; i++) {
 		    fprintf(stdout, "%.9f", gtod[i]);
 		    if (i < NSAMPLES - 1)
 			fputs(", ", stdout);
 		}
 		fputs("], \"Low\": [", stdout);
-		for (i = NBUF - NSAMPLES - 2; i < NBUF - 2; i++) {
+		for (int i = NBUF - NSAMPLES - 2; i < NBUF - 2; i++) {
 		    fprintf(stdout, "%.9f", gtod[i]);
 		    if (i < NBUF - 3)
 			fputs(", ", stdout);
@@ -141,10 +140,10 @@ void jitter(const iomode mode)
 		fprintf(stdout, "Mean %13.9Lf\n", average);
 		fprintf(stdout, "Jitter %13.9Lf\n", jitterx);
 		fprintf(stdout, "High\n");
-		for (i = 0; i < NSAMPLES; i++)
+		for (int i = 0; i < NSAMPLES; i++)
 		    fprintf(stdout, "%2d %13.9f\n", i, gtod[i]);
 		fprintf(stdout, "Low\n");
-		for (i = NBUF - NSAMPLES - 2; i < NBUF - 2; i++)
+		for (int i = NBUF - NSAMPLES - 2; i < NBUF - 2; i++)
 		    fprintf(stdout, "%2d %13.9f\n", i, gtod[i]);
 	}
 


=====================================
tests/libntp/macencrypt.c
=====================================
@@ -57,12 +57,11 @@ TEST(macencrypt, Encrypt) {
 		(uint32_t*)packetPtr, packetLength, length));
 
 if (0) {
-	int i;
         printf("\n");
-	for (i = 0; i< totalLength; i++)
+	for (int i = 0; i< totalLength; i++)
 	  printf("%02x ", (unsigned int)expectedMD5Packet[i] & 0xff);
         printf("\n");
-	for (i = 0; i< totalLength; i++)
+	for (int i = 0; i< totalLength; i++)
 	  printf("%02x ", (unsigned int)packetPtr[i] & 0xff);
         printf("\n");
 }
@@ -93,12 +92,11 @@ TEST(macencrypt, CMAC_Encrypt) {
 		(uint32_t*)packetPtr, packetLength, length));
 
 if (0) {
-	int i;
         printf("\n");
-	for (i = 0; i< totalLength; i++)
+	for (int i = 0; i< totalLength; i++)
 	  printf("%02x ", (unsigned int)expectedCMACPacket[i] & 0xff);
         printf("\n");
-	for (i = 0; i< totalLength; i++)
+	for (int i = 0; i< totalLength; i++)
 	  printf("%02x ", (unsigned int)packetPtr[i] & 0xff);
         printf("\n");
 }


=====================================
tests/libntp/ntp_random.c
=====================================
@@ -12,7 +12,6 @@ TEST_TEAR_DOWN(random) {}
 
 
 TEST(random, random32) {
-	int i;
 	uint32_t ones = 0;
 	uint32_t zeros = ~0;
 
@@ -22,7 +21,7 @@ TEST(random, random32) {
 	 * It won't be reproducible.  ;)
 	 * You can test this code by making the loop count smaller.
 	 */
-	for (i=0; i<99; i++) {
+	for (int i=0; i<99; i++) {
 		uint32_t sample = ntp_random();
 		ones |= sample;
 		zeros &= sample;
@@ -33,7 +32,6 @@ TEST(random, random32) {
 }
 
 TEST(random, random64) {
-	int i;
 	uint64_t ones = 0;
 	uint64_t zeros = ~0;
 
@@ -43,7 +41,7 @@ TEST(random, random64) {
 	 * It won't be reproducible.  ;)
 	 * You can test this code by making the loop count smaller.
 	 */
-	for (i=0; i<99; i++) {
+	for (int i=0; i<99; i++) {
 		uint64_t sample = ntp_random64();
 		ones |= sample;
 		zeros &= sample;


=====================================
tests/libntp/timespecops.c
=====================================
@@ -74,9 +74,7 @@ static bool AssertFpClose(const l_fp m, const l_fp n, const l_fp limit)
 static bool AssertTimespecClose(const struct timespec m, const struct timespec n,
 				const struct timespec limit)
 {
-	struct timespec diff;
-
-	diff = abs_tspec(sub_tspec(m, n));
+	struct timespec diff = abs_tspec(sub_tspec(m, n));
 	if (cmp_tspec(limit, diff) >= 0)
 		return true;
 	else
@@ -125,7 +123,6 @@ static uint32_t my_tick_to_tsf(uint32_t ticks)
 
 static uint32_t my_tsf_to_tick(uint32_t tsf)
 {
-
 	// Inverse operation: converts fraction to microseconds.
 	return (uint32_t)(( ((uint64_t)(tsf)) * 1000000000 + 0x80000000) >> 32);
 	// Beware: The result might be 10^9 due to rounding!
@@ -157,9 +154,7 @@ TEST(timespecops, Helpers1) {
 //----------------------------------------------------------------------
 
 TEST(timespecops, Normalise) {
-	long ns;
-
-	for ( ns = -2000000000; ns <= 2000000000; ns += 10000000) {
+	for (long ns = -2000000000; ns <= 2000000000; ns += 10000000) {
 		struct timespec x = timespec_init(0, ns);
 
 		x = normalize_tspec(x);
@@ -175,9 +170,7 @@ TEST(timespecops, Normalise) {
 
 TEST(timespecops, SignNoFrac) {
 	// sign test, no fraction
-	int i;
-
-	for (i = -4; i <= 4; ++i) {
+	for (int i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init(i, 0);
 		int E = (i > 0) - (i < 0);
 		int r = test_tspec_denorm(a);
@@ -191,9 +184,7 @@ TEST(timespecops, SignNoFrac) {
 
 TEST(timespecops, SignWithFrac) {
 	// sign test, with fraction
-	int i;
-
-	for (i = -4; i <= 4; ++i) {
+	for (int i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init(i, 10);
 		int E = (i >= 0) - (i < 0);
 		int r = test_tspec_denorm(a);
@@ -209,9 +200,8 @@ TEST(timespecops, SignWithFrac) {
 //----------------------------------------------------------------------
 TEST(timespecops, CmpFracEQ) {
 	// fractions are equal
-	int i, j;
-	for (i = -4; i <= 4; ++i)
-		for (j = -4; j <= 4; ++j) {
+	for (int i = -4; i <= 4; ++i)
+		for (int j = -4; j <= 4; ++j) {
 			struct timespec a = timespec_init( i , 200);
 			struct timespec b = timespec_init( j , 200);
 			int   E = (i > j) - (i < j);
@@ -225,10 +215,8 @@ TEST(timespecops, CmpFracEQ) {
 
 TEST(timespecops, CmpFracGT) {
 	// fraction a bigger fraction b
-	int i, j;
-
-	for (i = -4; i <= 4; ++i)
-		for (j = -4; j <= 4; ++j) {
+	for (int i = -4; i <= 4; ++i)
+		for (int j = -4; j <= 4; ++j) {
 			struct timespec a = timespec_init(i, 999999800);
 			struct timespec b = timespec_init(j, 200);
 			int   E = (i >= j) - (i < j);
@@ -242,10 +230,8 @@ TEST(timespecops, CmpFracGT) {
 
 TEST(timespecops, CmpFracLT) {
 	// fraction a less fraction b
-	int i, j;
-
-	for (i = -4; i <= 4; ++i)
-		for (j = -4; j <= 4; ++j) {
+	for (int i = -4; i <= 4; ++i)
+		for (int j = -4; j <= 4; ++j) {
 			struct timespec a = timespec_init(i, 200);
 			struct timespec b = timespec_init(j, 999999800);
 			int   E = (i > j) - (i <= j);
@@ -262,16 +248,12 @@ TEST(timespecops, CmpFracLT) {
 //----------------------------------------------------------------------
 
 TEST(timespecops, AddFullNorm) {
-	int i, j;
-
-	for (i = -4; i <= 4; ++i)
-		for (j = -4; j <= 4; ++j) {
+	for (int i = -4; i <= 4; ++i)
+		for (int j = -4; j <= 4; ++j) {
 			struct timespec a = timespec_init(i, 200);
 			struct timespec b = timespec_init(j, 400);
 			struct timespec E = timespec_init(i + j, 200 + 400);
-			struct timespec c;
-
-			c = add_tspec(a, b);
+			struct timespec c = add_tspec(a, b);
 			TEST_ASSERT_EQUAL_timespec(E, c);
 		}
 
@@ -280,16 +262,12 @@ TEST(timespecops, AddFullNorm) {
 
 
 TEST(timespecops, AddFullOflow1) {
-	int i, j;
-
-	for (i = -4; i <= 4; ++i)
-		for (j = -4; j <= 4; ++j) {
+	for (int i = -4; i <= 4; ++i)
+		for (int j = -4; j <= 4; ++j) {
 			struct timespec a = timespec_init(i, 200);
 			struct timespec b = timespec_init(j, 999999900);
 			struct timespec E = timespec_init(i + j + 1, 100);
-			struct timespec c;
-
-			c = add_tspec(a, b);
+			struct timespec c = add_tspec(a, b);
 			TEST_ASSERT_EQUAL_timespec(E, c);
 		}
 
@@ -298,14 +276,10 @@ TEST(timespecops, AddFullOflow1) {
 
 
 TEST(timespecops, AddNsecNorm) {
-	int i;
-
-	for (i = -4; i <= 4; ++i) {
+	for (int i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init(i, 200);
 		struct timespec E = timespec_init(i, 600);
-		struct timespec c;
-
-		c = add_tspec_ns(a, 600 - 200);
+		struct timespec c = add_tspec_ns(a, 600 - 200);
 		TEST_ASSERT_EQUAL_timespec(E, c);
 	}
 
@@ -314,14 +288,10 @@ TEST(timespecops, AddNsecNorm) {
 
 
 TEST(timespecops, AddNsecOflow1) {
-	int i;
-
-	for (i = -4; i <= 4; ++i) {
+	for (int i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init(i, 200);
 		struct timespec E = timespec_init(i + 1, 100);
-		struct timespec c;
-
-		c = add_tspec_ns(a, NS_PER_S - 100);
+		struct timespec c = add_tspec_ns(a, NS_PER_S - 100);
 		TEST_ASSERT_EQUAL_timespec(E, c);
 	}
 
@@ -333,16 +303,12 @@ TEST(timespecops, AddNsecOflow1) {
 //----------------------------------------------------------------------
 
 TEST(timespecops, SubFullNorm) {
-	int i, j;
-
-	for (i = -4; i <= 4; ++i)
-		for (j = -4; j <= 4; ++j) {
+	for (int i = -4; i <= 4; ++i)
+		for (int j = -4; j <= 4; ++j) {
 			struct timespec a = timespec_init( i , 600);
 			struct timespec b = timespec_init( j , 400);
 			struct timespec E = timespec_init(i-j, 200);
-			struct timespec c;
-
-			c = sub_tspec(a, b);
+			struct timespec c = sub_tspec(a, b);
 			TEST_ASSERT_EQUAL_timespec(E, c);
 		}
 
@@ -351,16 +317,12 @@ TEST(timespecops, SubFullNorm) {
 
 
 TEST(timespecops, SubFullOflow) {
-	int i, j;
-
-	for (i = -4; i <= 4; ++i)
-		for (j = -4; j <= 4; ++j) {
+	for (int i = -4; i <= 4; ++i)
+		for (int j = -4; j <= 4; ++j) {
 			struct timespec a = timespec_init(i, 100);
 			struct timespec b = timespec_init(j, 999999900);
 			struct timespec E = timespec_init(i - j - 1, 200);
-			struct timespec c;
-
-			c = sub_tspec(a, b);
+			struct timespec c = sub_tspec(a, b);
 			TEST_ASSERT_EQUAL_timespec(E, c);
 		}
 
@@ -369,14 +331,10 @@ TEST(timespecops, SubFullOflow) {
 
 
 TEST(timespecops, SubNsecNorm) {
-	int i;
-
-	for (i = -4; i <= 4; ++i) {
+	for (int i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init(i, 600);
 		struct timespec E = timespec_init(i, 200);
-		struct timespec c;
-
-		c = sub_tspec_ns(a, 600 - 200);
+		struct timespec c = sub_tspec_ns(a, 600 - 200);
 		TEST_ASSERT_EQUAL_timespec(E, c);
 	}
 
@@ -385,14 +343,10 @@ TEST(timespecops, SubNsecNorm) {
 
 
 TEST(timespecops, SubNsecOflow) {
-	int i;
-
-	for (i = -4; i <= 4; ++i) {
+	for (int i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init( i , 100);
 		struct timespec E = timespec_init(i-1, 200);
-		struct timespec c;
-
-		c = sub_tspec_ns(a, NS_PER_S - 100);
+		struct timespec c = sub_tspec_ns(a, NS_PER_S - 100);
 		TEST_ASSERT_EQUAL_timespec(E, c);
 	}
 
@@ -405,15 +359,10 @@ TEST(timespecops, SubNsecOflow) {
 
 
 TEST(timespecops, test_Neg) {
-	int i;
-
-	for (i = -4; i <= 4; ++i) {
+	for (int i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init(i, 100);
-		struct timespec b;
-		struct timespec c;
-
-		b = neg_tspec(a);
-		c = add_tspec(a, b);
+		struct timespec b = neg_tspec(a);
+		struct timespec c = add_tspec(a, b);
 		TEST_ASSERT_EQUAL(0, test_tspec(c));
 	}
 
@@ -425,13 +374,9 @@ TEST(timespecops, test_Neg) {
 //----------------------------------------------------------------------
 
 TEST(timespecops, test_AbsNoFrac) {
-	int i;
-
-	for (i = -4; i <= 4; ++i) {
+	for (int i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init(i , 0);
-		struct timespec b;
-
-		b = abs_tspec(a);
+		struct timespec b = abs_tspec(a);
 		TEST_ASSERT_EQUAL((i != 0), test_tspec(b));
 	}
 
@@ -440,13 +385,9 @@ TEST(timespecops, test_AbsNoFrac) {
 
 
 TEST(timespecops, test_AbsWithFrac) {
-	int i;
-
-	for (i = -4; i <= 4; ++i) {
+	for (int i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init(i, 100);
-		struct timespec b;
-
-		b = abs_tspec(a);
+		struct timespec b = abs_tspec(a);
 		TEST_ASSERT_EQUAL(1, test_tspec(b));
 	}
 
@@ -460,15 +401,14 @@ TEST(timespecops, test_AbsWithFrac) {
 /* FIXME: temporarily disabled - spews cryptic messages into test log */
 TEST(timespecops, test_Helpers2) {
 	struct timespec limit = timespec_init(0, 2);
-	struct timespec x, y;
-	long i;
+	struct timespec x;
 
 	for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++)
 		for (x.tv_nsec = 1;
 		     x.tv_nsec < 1000000000;
 		     x.tv_nsec += 499999999) {
-			for (i = -4; i < 5; ++i) {
-				y = x;
+			for (long i = -4; i < 5; ++i) {
+				struct timespec y = x;
 				y.tv_nsec += i;
 				if (i >= -2 && i <= 2) {
 					TEST_ASSERT_TRUE(AssertTimespecClose(x, y, limit));
@@ -489,14 +429,11 @@ TEST(timespecops, test_Helpers2) {
 
 TEST(timespecops, test_ToLFPbittest) {
 	l_fp lfpClose =  lfpinit(0, 1);
-	uint32_t i;
 
-	for (i = 0; i < 1000000000; i+=1000) {
+	for (uint32_t i = 0; i < 1000000000; i+=1000) {
 		struct timespec a = timespec_init(1, (long)i);
 		l_fp E= lfpinit(1, my_tick_to_tsf(i));
-		l_fp r;
-
-		r = tspec_intv_to_lfp(a);
+		l_fp r = tspec_intv_to_lfp(a);
 		TEST_ASSERT_TRUE(AssertFpClose(E, r, lfpClose));
 	}
 
@@ -505,14 +442,10 @@ TEST(timespecops, test_ToLFPbittest) {
 
 
 TEST(timespecops, test_ToLFPrelPos) {
-	int i;
-
-	for (i = 0; i < (int)COUNTOF(fdata); ++i) {
+	for (int i = 0; i < (int)COUNTOF(fdata); ++i) {
 		struct timespec a = timespec_init(1, fdata[i].nsec);
 		l_fp E = lfpinit(1, fdata[i].frac);
-		l_fp r;
-
-		r = tspec_intv_to_lfp(a);
+		l_fp r = tspec_intv_to_lfp(a);
 		TEST_ASSERT_EQUAL_l_fp(E, r);
 	}
 
@@ -521,14 +454,10 @@ TEST(timespecops, test_ToLFPrelPos) {
 
 
 TEST(timespecops, test_ToLFPrelNeg) {
-	int i;
-
-	for (i = 0; i < (int)COUNTOF(fdata); ++i) {
+	for (int i = 0; i < (int)COUNTOF(fdata); ++i) {
 		struct timespec a = timespec_init(-1, fdata[i].nsec);
 		l_fp E = lfpinit(~0, fdata[i].frac);
-		l_fp r;
-
-		r = tspec_intv_to_lfp(a);
+		l_fp r = tspec_intv_to_lfp(a);
 		TEST_ASSERT_EQUAL_l_fp(E, r);
 	}
 
@@ -537,14 +466,10 @@ TEST(timespecops, test_ToLFPrelNeg) {
 
 
 TEST(timespecops, test_ToLFPabs) {
-	int i;
-
-	for (i = 0; i < (int)COUNTOF(fdata); ++i) {
+	for (int i = 0; i < (int)COUNTOF(fdata); ++i) {
 		struct timespec a = timespec_init(1, fdata[i].nsec);
 		l_fp E = lfpinit((int)(1 + JAN_1970), fdata[i].frac);
-		l_fp r;
-
-		r = tspec_stamp_to_lfp(a);
+		l_fp r = tspec_stamp_to_lfp(a);
 		TEST_ASSERT_EQUAL_l_fp(E, r);
 	}
 
@@ -561,13 +486,10 @@ TEST(timespecops, test_FromLFPbittest) {
 	// Not *exactly* a bit test, because 2**32 tests would take a
 	// really long time even on very fast machines! So we do test
 	// every 1000 fractional units.
-	uint32_t tsf;
-	for (tsf = 0; tsf < ~((uint32_t)(1000)); tsf += 1000) {
+	for (uint32_t tsf = 0; tsf < ~((uint32_t)(1000)); tsf += 1000) {
 		struct timespec E = timespec_init(1, (long)my_tsf_to_tick(tsf));
 		l_fp a = lfpinit(1, tsf);
-		struct timespec r;
-
-		r = lfp_intv_to_tspec(a);
+		struct timespec r = lfp_intv_to_tspec(a);
 		// The conversion might be off by one nanosecond when
 		// comparing to calculated value.
 		TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
@@ -579,14 +501,11 @@ TEST(timespecops, test_FromLFPbittest) {
 
 TEST(timespecops, test_FromLFPrelPos) {
 	struct timespec limit = timespec_init(0, 2);
-	int i;
 
-	for (i = 0; i < (int)COUNTOF(fdata); ++i) {
+	for (int i = 0; i < (int)COUNTOF(fdata); ++i) {
 		l_fp a = lfpinit(1, fdata[i].frac);
 		struct timespec E = timespec_init(1, fdata[i].nsec);
-		struct timespec r;
-
-		r = lfp_intv_to_tspec(a);
+		struct timespec r = lfp_intv_to_tspec(a);
 		TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
 	}
 
@@ -596,14 +515,11 @@ TEST(timespecops, test_FromLFPrelPos) {
 
 TEST(timespecops, test_FromLFPrelNeg) {
 	struct timespec limit = timespec_init(0, 2);
-	int i;
 
-	for (i = 0; i < (int)COUNTOF(fdata); ++i) {
+	for (int i = 0; i < (int)COUNTOF(fdata); ++i) {
 		l_fp a = lfpinit(~0, fdata[i].frac);
 		struct timespec E = timespec_init(-1, fdata[i].nsec);
-		struct timespec r;
-
-		r = lfp_intv_to_tspec(a);
+		struct timespec r = lfp_intv_to_tspec(a);
 		TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
 	}
 
@@ -613,17 +529,11 @@ TEST(timespecops, test_FromLFPrelNeg) {
 
 // nsec -> frac -> nsec roundtrip, using a prime start and increment
 TEST(timespecops, test_LFProundtrip) {
-	int32_t t;
-	uint32_t i;
-
-	for (t = -1; t < 2; ++t)
-		for (i = 4999; i < 1000000000; i += 10007) {
+	for (int32_t t = -1; t < 2; ++t)
+		for (uint32_t i = 4999; i < 1000000000; i += 10007) {
 			struct timespec E = timespec_init(t, (long)i);
-			l_fp a;
-			struct timespec r;
-
-			a = tspec_intv_to_lfp(E);
-			r = lfp_intv_to_tspec(a);
+			l_fp a = tspec_intv_to_lfp(E);
+			struct timespec r = lfp_intv_to_tspec(a);
 			TEST_ASSERT_EQUAL_timespec(E, r);
 		}
 
@@ -640,13 +550,10 @@ TEST(timespecops, test_FromLFPuBittest) {
 	// Not *exactly* a bit test, because 2**32 tests would take a
 	// really long time even on very fast machines! So we do test
 	// every 1000 fractional units.
-	uint32_t tsf;
-	for (tsf = 0; tsf < ~((uint32_t)(1000)); tsf += 1000) {
+	for (uint32_t tsf = 0; tsf < ~((uint32_t)(1000)); tsf += 1000) {
 		struct timespec E = timespec_init(1, (long)my_tsf_to_tick(tsf));
 		l_fp a = lfpinit(1, tsf);
-		struct timespec r;
-
-		r = lfp_uintv_to_tspec(a);
+		struct timespec r = lfp_uintv_to_tspec(a);
 		// The conversion might be off by one nanosecond when
 		// comparing to calculated value.
 		TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
@@ -658,14 +565,11 @@ TEST(timespecops, test_FromLFPuBittest) {
 
 TEST(timespecops, test_FromLFPuRelPos) {
 	struct timespec limit = timespec_init(0, 2);
-	int i;
 
-	for (i = 0; i < (int)COUNTOF(fdata); ++i) {
+	for (int i = 0; i < (int)COUNTOF(fdata); ++i) {
 		l_fp a = lfpinit(1, fdata[i].frac);
 		struct timespec E = timespec_init(1, fdata[i].nsec);
-		struct timespec r;
-
-		r = lfp_uintv_to_tspec(a);
+		struct timespec r = lfp_uintv_to_tspec(a);
 		TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
 	}
 
@@ -675,14 +579,11 @@ TEST(timespecops, test_FromLFPuRelPos) {
 
 TEST(timespecops, test_FromLFPuRelNeg) {
 	struct timespec limit = timespec_init(0, 2);
-	int i;
 
-	for (i = 0; i < (int)COUNTOF(fdata); ++i) {
+	for (int i = 0; i < (int)COUNTOF(fdata); ++i) {
 		l_fp a = lfpinit(~0, fdata[i].frac);
 		struct timespec E = timespec_init(-1, fdata[i].nsec);
-		struct timespec r;
-
-		r = lfp_uintv_to_tspec(a);
+		struct timespec r = lfp_uintv_to_tspec(a);
 		TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
 	}
 
@@ -692,17 +593,11 @@ TEST(timespecops, test_FromLFPuRelNeg) {
 
 // nsec -> frac -> nsec roundtrip, using a prime start and increment
 TEST(timespecops, test_LFPuRoundtrip) {
-	int32_t t;
-	uint32_t i;
-
-	for (t = -1; t < 2; ++t)
-		for (i = 4999; i < 1000000000; i += 10007) {
+	for (int32_t t = -1; t < 2; ++t)
+		for (uint32_t i = 4999; i < 1000000000; i += 10007) {
 			struct timespec E = timespec_init(t, (long)i);
-			l_fp a;
-			struct timespec r;
-
-			a = tspec_intv_to_lfp(E);
-			r = lfp_uintv_to_tspec(a);
+			l_fp a = tspec_intv_to_lfp(E);
+			struct timespec r = lfp_uintv_to_tspec(a);
 			TEST_ASSERT_EQUAL_timespec(E, r);
 		}
 
@@ -714,9 +609,7 @@ TEST(timespecops, test_LFPuRoundtrip) {
 //----------------------------------------------------------------------
 
 TEST(timespecops, DToTspec) {
-	struct timespec res;
-
-	res = d_to_tspec(42.25);
+	struct timespec res = d_to_tspec(42.25);
 	TEST_ASSERT_EQUAL(42, res.tv_sec);
 	TEST_ASSERT_EQUAL(250000000, res.tv_nsec);
 }
@@ -726,9 +619,7 @@ TEST(timespecops, DToTspec) {
 //----------------------------------------------------------------------
 
 TEST(timespecops, LfpStampToTspec) {
-	struct timespec res;
-
-	res = lfp_stamp_to_tspec(86400, 100000);
+	struct timespec res = lfp_stamp_to_tspec(86400, 100000);
 	TEST_ASSERT_EQUAL(2085978496, res.tv_sec);
 	TEST_ASSERT_EQUAL(20117, res.tv_nsec);
 }
@@ -738,12 +629,11 @@ TEST(timespecops, LfpStampToTspec) {
 //----------------------------------------------------------------------
 
 TEST(timespecops, TvalToTspec) {
-	struct timespec res;
 	struct timeval in;
 
 	in.tv_sec = 42;
 	in.tv_usec = 23;
-	res = tval_to_tspec(in);
+	struct timespec res = tval_to_tspec(in);
 	TEST_ASSERT_EQUAL(42, res.tv_sec);
 	TEST_ASSERT_EQUAL(23000, res.tv_nsec);
 }



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/b5dfa19baff4e47e0323f84ce8ecfcf31e305d13

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/b5dfa19baff4e47e0323f84ce8ecfcf31e305d13
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/20190204/fca75b59/attachment-0001.html>


More information about the vc mailing list