Menu

false positive - assuming division by zero for 'num % radio' when num can be zro.

2023-01-18
2023-01-18
  • Mario De Weerd

    Mario De Weerd - 2023-01-18

    Cppcheck 2.7

    'num' is never the divider in this code yet a division by zero due to num is deemed possible because of the test for '!num'.

    Called with
    cppcheck --inline-suppr --enable=all --inconclusive --suppress=unusedFunction -Duint8=unsigned '-Dxuint8=unsigned char' -Duint16=unsigned '-Dxuint16=unsigned short' ./tmp.c

    tmp.c:5:13: warning: Either the condition '!num' is redundant or there is division by zero at line 5. [zerodivcond]
        c = num % radix;
                ^
    tmp.c:7:10: note: Assuming that condition '!num' is not redundant
        if ( !num )
             ^
    tmp.c:6:12: note: Assignment to 'num/=radix'
        num /= radix;
               ^
    tmp.c:5:13: note: Division by zero
        c = 
    
    void tmp(uint16 num, uint8 radix)
    {
      for ( i=0; i<5; i++)
      {
        c = num % radix;  // Isolate a digit
        num /= radix;
        if ( !num )
          break;
      }
    }
    
     
  • CHR

    CHR - 2023-01-18

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/11505

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.