eal: simplify code to avoid Coverity false positive

Message ID 1744662518-5433-1-git-send-email-andremue@linux.microsoft.com (mailing list archive)
State New
Delegated to: David Marchand
Headers
Series eal: simplify code to avoid Coverity false positive |

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/github-robot: build success github build: passed
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-abi-testing success Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-mellanox-Functional success Functional Testing PASS

Commit Message

Andre Muezerie April 14, 2025, 8:28 p.m. UTC
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.

Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
 lib/eal/common/eal_common_string_fns.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
  

Patch

diff --git a/lib/eal/common/eal_common_string_fns.c b/lib/eal/common/eal_common_string_fns.c
index 3bbd325515..fa87831c3a 100644
--- a/lib/eal/common/eal_common_string_fns.c
+++ b/lib/eal/common/eal_common_string_fns.c
@@ -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;
 }