[Git][NTPsec/ntpsec][master] Replace some strlen() calls to pacify linters.

Richard Laager (@rlaager) gitlab at mg.gitlab.com
Thu Jun 26 09:45:10 UTC 2025



Richard Laager pushed to branch master at NTPsec / ntpsec


Commits:
054a8049 by James Browning at 2025-06-25T18:44:14-07:00
Replace some strlen() calls to pacify linters.

Codacy does not like the lack of maximum length in strlen()

- - - - -


8 changed files:

- attic/sign-timing.c
- libntp/decodenetnum.c
- libntp/getopt.c
- libntp/isc_interfaceiter.c
- libntp/msyslog.c
- libntp/numtoa.c
- libntp/statestr.c
- ntpd/keyword-gen.c


Changes:

=====================================
attic/sign-timing.c
=====================================
@@ -143,14 +143,13 @@ static void DoSign(
   char stupid_api1[20];
   char stupid_api2[50];
 
-  const char* context = "RoughTime v1 delegation signature-";
-  int contextlen = strlen(context)+1;
+  const char context[] = "RoughTime v1 delegation signature-";
   OSSL_PARAM params[3];
 
   strncpy(stupid_api1, "Ed25519ctx", sizeof(stupid_api1));
   params[0] = OSSL_PARAM_construct_utf8_string("instance", stupid_api1, 0);
   strncpy(stupid_api2, context, sizeof(stupid_api2));
-  params[1] = OSSL_PARAM_construct_octet_string("context-string", stupid_api2, contextlen);
+  params[1] = OSSL_PARAM_construct_octet_string("context-string", stupid_api2, sizeof(context));
   params[2] = OSSL_PARAM_construct_end();
   err = EVP_DigestSignInit_ex(ctx, NULL, NULL, NULL, NULL, key, params);
   if (1 != err ) {


=====================================
libntp/decodenetnum.c
=====================================
@@ -55,7 +55,7 @@ decodenetnum(
 	if ( NULL == num) {
                 return -4001;
         }
-	numlen = strlen(num);
+	numlen = strnlen(num, INET6_ADDRSTRLEN + NI_MAXSERV + 2); // 80
 	/* Quickly reject empty or impossibly long inputs. */
 	if(numlen == 0 ||
 	   numlen > ((sizeof(ip) - 1) + (NI_MAXSERV - 1) + (3 /* "[]:" */))) {


=====================================
libntp/getopt.c
=====================================
@@ -161,7 +161,7 @@ int ntp_getopt_long(int argc, char *const argv[], const char *optstring,
 		return -1;
 	}
 
-	if (strlen(argv[ntp_optind]) < 3 || strncmp(argv[ntp_optind], "--", 2) != 0) {
+	if (strnlen(argv[ntp_optind], 3) < 3 || strncmp(argv[ntp_optind], "--", 2) != 0) {
 		return ntp_getopt(argc, argv, optstring);
 	}
 


=====================================
libntp/isc_interfaceiter.c
=====================================
@@ -364,7 +364,7 @@ internal_current(isc_interfaceiter_t *iter) {
 
         memset(&iter->current, 0, sizeof(iter->current));
 
-        namelen = (unsigned int)strlen(ifa->ifa_name);
+        namelen = (unsigned int)strnlen(ifa->ifa_name, sizeof(iter->current.name));
         if (namelen > sizeof(iter->current.name) - 1)
                 namelen = sizeof(iter->current.name) - 1;
 
@@ -1571,7 +1571,7 @@ linux_if_inet6_current(isc_interfaceiter_t *iter) {
 */
                 return (ISC_R_FAILURE);
         }
-        if (strlen(address) != 32) {
+        if (strnlen(address, 33) != 32) {
 /*              isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
                               ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
                               "/proc/net/if_inet6:strlen(%s) != 32", address);


=====================================
libntp/msyslog.c
=====================================
@@ -381,7 +381,7 @@ change_logfile(
 		    0 != strcmp(log_fname, "stderr") &&
 		    0 != strcmp(log_fname, "stdout") &&
 		    NULL != getcwd(curdir, sizeof(curdir))) {
-			cd_octets = strlen(curdir);
+			cd_octets = strnlen(curdir, sizeof(curdir));
 			/* trim any trailing '/' */
 			if (cd_octets > 1 &&
 			    DIR_SEP == curdir[cd_octets - 1])


=====================================
libntp/numtoa.c
=====================================
@@ -22,7 +22,6 @@ refid_str(
 	)
 {
 	char *	text;
-	size_t	tlen;
 
 	if (stratum > 1) {
 		struct in_addr in4;
@@ -38,8 +37,7 @@ refid_str(
 	  if (text[i] != ' ') break;
 	  text[i] = '\0';
 	}
-	tlen = strlen(text);
-	if (0 == tlen) {
+	if ('\0' == *text) {
 	  strlcat(text, "?", LIB_BUFLENGTH);
 	}
 


=====================================
libntp/statestr.c
=====================================
@@ -413,9 +413,9 @@ statustoa(
 			 getcode((st) & 0xf, clock_codes));
 		break;
 
-        default:
-                /* huh? */
-                break;
+	default:
+		/* huh? */
+		break;
 	}
 
 	return cb;


=====================================
ntpd/keyword-gen.c
=====================================
@@ -702,7 +702,7 @@ populate_symb(
 		    && token < (int)COUNTOF(symb)) {
 
 			symb[token] = strdup(name);
-			if (strlen(name) > MAX_TOK_LEN) {
+			if (strnlen(name, MAX_TOK_LEN + 1) > MAX_TOK_LEN) {
 				fprintf(stderr,
 					"MAX_TOK_LEN %d too small for '%s'\n"
 					"Edit keyword-gen.c to raise.\n",



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/commit/054a80492c40effe2eb97cd8e06b03df331bde0f

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/commit/054a80492c40effe2eb97cd8e06b03df331bde0f
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/20250626/a09a12fb/attachment-0001.htm>


More information about the vc mailing list