From 7a42d4f0e5f40f18d395cfc7881f70c44c1193f1 Mon Sep 17 00:00:00 2001
From: Andreas Mohr <and@gmx.li>
Date: Thu, 19 Dec 2024 20:00:00 +0000
Subject: [PATCH] (util.c) fix Undefined Binary Operator Result
lib/util.c:493:28: warning: The left operand of '!=' is a garbage value due to array index out of bounds [clang-analyzer-core.UndefinedBinaryOperatorResult]
493 | for (j = units; sfx[j] != NULL; j++)
| ~~~~~~ ^
- verify and limit input 'units' value
Found by Clang-19 Static Analyzer
Signed-off-by: Andreas Mohr <and@gmx.li>
---
lib/util.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/lib/util.c b/lib/util.c
index ecc219ed8..a5a977312 100644
a
|
b
|
size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gbool |
460 | 460 | /* *INDENT-ON* */ |
461 | 461 | |
462 | 462 | const char *const *sfx = use_si ? suffix_lc : suffix; |
| 463 | int sfx_count = 0; |
463 | 464 | int j = 0; |
464 | 465 | |
| 466 | while (sfx[sfx_count]) { |
| 467 | sfx_count++; |
| 468 | } |
| 469 | |
465 | 470 | if (len == 0) |
466 | 471 | len = 9; |
467 | 472 | #if SIZEOF_UINTMAX_T == 8 |
… |
… |
size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gbool |
480 | 485 | * if uintmax_t type is too small |
481 | 486 | */ |
482 | 487 | if (use_si) |
483 | | for (j = 0; j < units; j++) |
| 488 | for (j = 0; j < MIN (units, sfx_count - 1); j++) |
484 | 489 | { |
485 | 490 | uintmax_t size_remain; |
486 | 491 | |
… |
… |
size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gbool |
490 | 495 | size += size_remain; /* Re-add remainder lost by division/multiplication */ |
491 | 496 | } |
492 | 497 | |
493 | | for (j = units; sfx[j] != NULL; j++) |
| 498 | for (j = MIN (units, sfx_count - 1); sfx[j] != NULL; j++) |
494 | 499 | { |
495 | 500 | if (size == 0) |
496 | 501 | { |