num2str(): Avoid an out-of-bounds array access
authorBart Van Assche <bvanassche@acm.org>
Fri, 7 Sep 2018 14:19:34 +0000 (07:19 -0700)
committerBart Van Assche <bvanassche@acm.org>
Fri, 7 Sep 2018 14:29:35 +0000 (07:29 -0700)
This patch fixes Coverity ID 169307.

Fixes: 1ec3d69b0ed8 ("Implement a better num2str()")
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
lib/num2str.c

index ff8a365bcaed516e07c69aac91f96e73952faa2b..1abe22f33794c0ccf6b724e74aef7a1f62271166 100644 (file)
@@ -72,7 +72,7 @@ char *num2str(uint64_t num, int maxlen, int base, int pow2, enum n2s_unit units)
         * Divide by K/Ki until string length of num <= maxlen.
         */
        modulo = -1U;
         * Divide by K/Ki until string length of num <= maxlen.
         */
        modulo = -1U;
-       while (post_index < sizeof(sistr)) {
+       while (post_index < ARRAY_SIZE(sistr)) {
                sprintf(tmp, "%llu", (unsigned long long) num);
                if (strlen(tmp) <= maxlen)
                        break;
                sprintf(tmp, "%llu", (unsigned long long) num);
                if (strlen(tmp) <= maxlen)
                        break;