Menu

Spurious nullPointerRedundantCheck

James Bray
2025-10-17
2025-10-17
  • James Bray

    James Bray - 2025-10-17

    Hi,

    I have the following code:

    void func( )
    {
       char *text = strdup("test")
       if (!text) goto END;
    
       if (strcmp(text, "test"))
           printf("Different\n");
    
       if (text) free(text);
    
       return;
    
    END:
    }
    

    When running using 2.18 or the latest code from 17-Oct-25, I get the following spurious warnings:

    test.c:6:15: warning: Either the condition 'text' is redundant or there is possible null pointer dereference: text. [nullPointerRedundantCheck]
    if (strcmp(text, "test"))
    ^
    test.c:9:8: note: Assuming that condition 'text' is not redundant
    if (text) free(text);
    ^
    test.c:6:15: note: Null pointer dereference
    if (strcmp(text, "test"))

    If I remove the final "if (text)" condition, it no longer generates the warnings. I would not expect that redundant condition to cause warnings in the strcmp call at the beginning.

    This didn't occur in 2.17, and at the moment, I'm forced to suppress that particular check.

     
  • CHR

    CHR - 2025-10-17

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14214
    Maybe you can avoid the warning by adding --check-level=exhaustive to your command line.

     
  • James Bray

    James Bray - 2025-10-17

    Hi, thanks for the suggestion. That does remove the error, but unfortunately a trivial change re-introduces it:

    void func( )
    {
       char *text = strdup("test")
       if (!text) goto END;
    
       if (strcmp(text, "test"))
           goto END;
    
       if (text) free(text);
    
       return;
    
    END:
    }
    

    Replacing the "printf" with "goto END" in the strcmp conditional causes the nullPointerRedundantCheck to occur even with --check-level=exhaustive

    For the moment I will simply disable this warning.

     

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.