Skip to contentSkip to conversation

eal: simplify code to avoid Coverity false positive

PATCH Applied Andre Muezerie 2 participants 2 messages Apr 14, 2025 – May 15, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal: simplify code to avoid Coverity false positive

Coverity complained about an overflow in a recently added function:

https://scan4.scan.coverity.com/#/project-view/66295/10075?selectedIssue=461876

CID 461876: (#1 of 1): Overflowed constant (INTEGER_OVERFLOW)
21. overflow_const: Expression powi, which is equal to 0, where base is known to be equal to 1024, overflows the type that receives it, an unsigned integer 64 bits wide.

This complaint was a false positive, but it revealed that the function could be written in a simpler way, making the code more readable and likely allowing Coverity to make the right determination.

---
 lib/eal/common/eal_common_string_fns.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
lib/eal/common/eal_common_string_fns.c +4 -7
index 3bbd325515..fa87831c3a 100644
@@ -115,7 +115,8 @@ char *
 rte_size_to_str(char *buf, int buf_size, uint64_t count, bool use_iec, const char *unit)
 {
 	/* https://en.wikipedia.org/wiki/International_System_of_Units */
-	const char *prefix = "kMGTPE";
+	static const char prefix[] = "kMGTPE";
+	size_t prefix_index = 0;
 	const unsigned int base = use_iec ? 1024 : 1000;
 	uint64_t powi = 1;
 	uint16_t powj = 1;
@@ -134,14 +135,10 @@ rte_size_to_str(char *buf, int buf_size, uint64_t count, bool use_iec, const cha
 	/* increase value by a factor of 1000/1024 and store
 	 * if result is something a human can read
 	 */
-	for (;;) {
+	for (; prefix_index < sizeof(prefix) - 1; ++prefix_index) {
 		powi *= base;
 		if (count / powi < base)
 			break;
-
-		if (prefix[1] == '\0')
-			break;
-		++prefix;
 	}
 
 	/* try to guess a good number of digits for precision */
@@ -152,7 +149,7 @@ rte_size_to_str(char *buf, int buf_size, uint64_t count, bool use_iec, const cha
 	}
 
 	result = snprintf(buf, buf_size, "%.*f %c%s%s", precision,
-			  (double)count / powi, *prefix, use_iec ? "i" : "",
+			  (double)count / powi, prefix[prefix_index], use_iec ? "i" : "",
 			  (unit != NULL) ? unit : "");
 	return result < buf_size ? buf : NULL;
 }
Signature · 1 line
2.49.0.vfs.0.0
Reviewed-by on v1 1/1 Applied
#1 lore raw
Quoted 10 lines · Andre Muezerie

Coverity complained about an overflow in a recently added function:

https://scan4.scan.coverity.com/#/project-view/66295/10075?selectedIssue=461876

CID 461876: (#1 of 1): Overflowed constant (INTEGER_OVERFLOW)
21. overflow_const: Expression powi, which is equal to 0, where base is known to be equal to 1024, overflows the type that receives it, an unsigned integer 64 bits wide.

Quoted 5 lines

This complaint was a false positive, but it revealed that the function could be written in a simpler way, making the code more readable and likely allowing Coverity to make the right determination.

Applied, thanks.

Signature · 1 line
David Marchand