Menu

False positive on misra-c2012-8.7

2022-02-16
2022-03-13
  • Henrik Nyholm

    Henrik Nyholm - 2022-02-16

    Hi, I think there is a false positive on misra-c2012-8.7 in the following scenario:

    //foo.h
    void foo(void);
    
    //foo.c
    #include "foo.h"
    
    void foo(void)
    {
        return; 
    }
    
    //main.c
    #include "foo.h"
    
    int main(void)
    {   
        foo(); 
        return 0; 
    }
    

    Using ccpcheck on main.c results in the following output:

    foo.h:1:6: [misra-c2012-8.7] Advisory: Functions and objects should not be defined with external linkage if the are referenced in only one translation unit
    void foo(void);
    ^

    Here, the function is part of an API for foo.c and it is called in main.c.
    If you look at this thread on the misra forum, the official answer to a similar suggestion of a false positive from misra is that:

    The term "referenced" in the headline includes both the a call of a function and the definition of a function. A function which is defined in one translation unit and called in a different translation unit is compliant with this rule.
    An example of this compliant situation occurs in the example suite. Function R_8_7_2 is declared in a header (R_08_07.h), defined in R_08_07_2.c and called in R_08_07_1.c.

    Thus, I suggest modifying the checker to only trigger a fault of 8.7 when a function is not called from a different translation unit. Thoughts on this?

    Thanks!

     
  • Henrik Nyholm

    Henrik Nyholm - 2022-02-16

    (also, there is a small typo: ...if the are referenced... --> if they are referenced)

     
  • Daniel Marjamäki

    Thanks! Good input. I created ticket https://trac.cppcheck.net/ticket/10814

     
    • José Martins

      José Martins - 2022-02-19

      Daniel, just to note the ticket has a typo: the reported false positive is regarding rule 8.7, not rule 8.2

       
  • Henrik Nyholm

    Henrik Nyholm - 2022-02-17

    Thanks!

     
  • José Martins

    José Martins - 2022-02-19

    Henrik, I've submitted a pull request which I believe fixes this false positive as well as a couple others. If you could please make sure it also fixes the FPs from your end, it would be much appreciated.

     
    👍
    1

    Last edit: José Martins 2022-02-19
  • Daniel Marjamäki

    I have closed https://trac.cppcheck.net/ticket/10814 because I can't reproduce the false positive with the given code.

     
    👍
    1

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.