Menu

False positive on misra-c2012-5.9

2022-02-18
2022-03-13
  • José Martins

    José Martins - 2022-02-18

    The exception to the MISRA rule 5.9 described in the guidelines, regarding static inlined functions in header files, is not taken into account by the cppcheck misra add-on. For example, when using the following files:

    // file a.h
    static inline int f(void) {
        return;
    }
    
    // file b.c
    #include "a.h"
    
    // file c.c
    #include "a.h"
    

    Running cppcheck with the misra add-on triggers a rule 5.9 violation when I believe it shoulnd't:

    [23:29] misratests | cppcheck --addon=misra a.h b.c c.c
    Checking a.h ...
    1/3 files checked 57% done
    Checking b.c ...
    2/3 files checked 78% done
    Checking c.c ...
    3/3 files checked 100% done
    a.h:1:19: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-5.9]
    static inline int f(void){
    

    Best regards,
    José

     
  • Daniel Marjamäki

    Thanks! I have created https://trac.cppcheck.net/ticket/10820

    cppcheck --addon=misra a.h b.c c.c

    I do not recommend that you run cppcheck on the headers directly.

    The command cppcheck --addon=misra b.c c.c should be used instead. since b.c and c.c include a.h , the header will be checked in those translation units more properly.

    If you check a header directly you can for instance expect false positives for unusedStructMember.

     
    • José Martins

      José Martins - 2022-02-19

      Thank you for bringing it to my attention Daniel! I've been passing headers to cppcheck, both with and without misra add-on, since I started using it.

      Nevertheless, the issue it's still there if only passing the c files. I took a look at the code yesterday, and I believe it is a simple matter to fix. I'll probably submit a PR later today.

       
      • Daniel Marjamäki

        I assume you fixed https://trac.cppcheck.net/ticket/10820 ? I can't reproduce the FP right now. I have closed it.

         
  • José Martins

    José Martins - 2022-02-19

    While trying to fix this issue, I came across another issue where sometimes the inline keyword is ignored when we use a multiple word return type (e.g. unsigned long). For example, using misra rule 8.10 for illustration purposes:

    //file r8_10.c
    inline long f(void) { return; }
    

    Running cppcheck --addon=misra r8_10.c correctly yields:

    Checking r8_10.c ...
    r8_10.c:2:13: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-8.10]
    inline long f(void);
    

    However, a slight change to the file to use a multi-word return type:

    //file r8_10.c
    inline unsigned long f(void) { return; }
    

    No longer triggers any violation.

    I believe I also have a suggestion to patch this issue, although I'm not completely confident it is the best approach. PR later.

     

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.