Menu

False positive

2026-02-11
2026-02-13
  • Gleb Plekhotko

    Gleb Plekhotko - 2026-02-11

    Hello to community!

    I'm not sure this particular case worth to be included into the cppcheck, but still reporting it. Here is the simplified peace of code:

    int *ptr;
    
    int main(void) 
    {
        if (ptr != NULL) {
             while (1);
         }
    
      printf("%d", *ptr);
    }
    

    For this case, cppcheck reports the nullPointerRedundantCheck id, considering, as far as I understand, that there is a chance to dereference the null pointer. Though it is impossible, as the program gets stuck in the eternal while loop.

    This is not a typical scenario for the PC programs, but quite ubiquitous in the embedded world, where the piece of code above eventually triggers the watchdog or some other protecting feature, causing a program to restore its behavior.

    Thanks for your attention.

     
  • CHR

    CHR - 2026-02-13

    If ptr is NULL in main(), the while loop is not entered, and the pointer will be dereferenced. So the warning seems correct to me.

     

Log in to post a comment.

MongoDB Logo MongoDB