From: Andre Muezerie <andremue@linux.microsoft.com>
To: Tyler Retzlaff <roretzla@linux.microsoft.com>
Cc: dev@dpdk.org, Andre Muezerie <andremue@linux.microsoft.com>
Subject: [PATCH] eal: simplify code to avoid Coverity false positive
Date: Mon, 14 Apr 2025 13:28:38 -0700 [thread overview]
Message-ID: <1744662518-5433-1-git-send-email-andremue@linux.microsoft.com> (raw)
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(-)
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;
}
--
2.49.0.vfs.0.0
reply other threads:[~2025-04-14 20:28 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1744662518-5433-1-git-send-email-andremue@linux.microsoft.com \
--to=andremue@linux.microsoft.com \
--cc=dev@dpdk.org \
--cc=roretzla@linux.microsoft.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).