Ticket #4572: mc-4572-util_c-fix-undefined-binary-operator-result.patch

File mc-4572-util_c-fix-undefined-binary-operator-result.patch, 1.7 KB (added by and, 10 hours ago)
  • lib/util.c

    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 
    460460    /* *INDENT-ON* */ 
    461461 
    462462    const char *const *sfx = use_si ? suffix_lc : suffix; 
     463    int sfx_count = 0; 
    463464    int j = 0; 
    464465 
     466    while (sfx[sfx_count]) { 
     467        sfx_count++; 
     468    } 
     469 
    465470    if (len == 0) 
    466471        len = 9; 
    467472#if SIZEOF_UINTMAX_T == 8 
    size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gbool 
    480485     * if uintmax_t type is too small 
    481486     */ 
    482487    if (use_si) 
    483         for (j = 0; j < units; j++) 
     488        for (j = 0; j < MIN (units, sfx_count - 1); j++) 
    484489        { 
    485490            uintmax_t size_remain; 
    486491 
    size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gbool 
    490495            size += size_remain;        /* Re-add remainder lost by division/multiplication */ 
    491496        } 
    492497 
    493     for (j = units; sfx[j] != NULL; j++) 
     498    for (j = MIN (units, sfx_count - 1); sfx[j] != NULL; j++) 
    494499    { 
    495500        if (size == 0) 
    496501        {