Menu ▾ ▴

staticFunction ignores an external function-address comparison

Long Huang
6 days ago
46 minutes ago
  • Long Huang

    Long Huang - 6 days ago

    Reproduction

    handler.c:

    void DefaultHandler(void)
    {
    }
    
    void (*const Handlers[])(void) = { DefaultHandler };
    

    compare.c:

    extern void DefaultHandler(void);
    extern void (*const Handlers[])(void);
    
    int HasHandler(int index)
    {
       return Handlers[index] != DefaultHandler;
    }
    

    Run both translation units together:

    cppcheck --enable=all --inline-suppr --template=gcc \
      --suppress=checkersReport --suppress=unusedFunction --quiet \
      handler.c compare.c
    

    Cppcheck 2.22.0

    Actual:

    handler.c:1:6: warning: The function 'DefaultHandler' should have static linkage since it is not used outside of its translation unit. [staticFunction]
    

    Expected:

    No warning for DefaultHandler. compare.c takes its address to
    compare against the exported table. Giving the definition internal linkage
    would leave that external symbol reference unresolved. The function need not
    be called directly to require external linkage.

     
  • CHR

    CHR - 46 minutes ago

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

     

Log in to post a comment.