Menu

False positive null pointer deref

NRK
2022-09-07
2022-09-13
  • NRK

    NRK - 2022-09-07

    Test-case (tested with cppcheck v2.8.2 as well as main branch commit 5477130):

    int
    f(void)
    {
        if (0)
            *(int *)0 = 1;
        return 0 ? *(int *)0 : 1;
    }
    

    This raises the following warnings:

    $ ./cppcheck /tmp/test.c
    Checking /tmp/test.c ...
    /tmp/test.c:5:4: error: Null pointer dereference: (int*)0 [nullPointer]
      *(int *)0 = 1;
       ^
    /tmp/test.c:6:14: error: Null pointer dereference: (int*)0 [nullPointer]
     return 0 ? *(int *)0 : 1;
                 ^
    

    Use-case: this false positive was detected when used on a macro which takes an optional pointer. Something along the lines of:

    #define optional_ret(X) do { \
        /* do stuff */ \
        (X) ? (*(int *)(X) = 5) : 0; \
    } while (0)
    
    void f2(void) { optional_ret(NULL); }
    
     
  • CHR

    CHR - 2022-09-13

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

     

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.