Menu

Misra 17.7 doesn't detect some cases

2020-09-04
2020-09-21
  • Jean-Loic MARTIN

    Hi all,
    I am having troubles with Misra rules 17.7 checks.

    The Misra add-on detects that memset returns code is not tested and generates an error => Good.
    But it doesn't detects that memcpy retunr code isn't tested => Not good.

    The code:

    void main( CHAR* pacLongitude )
    {
        memcpy( acBufferTemp , (const void*) ( pacLongitude ) , 3 );
        u8Degrees = (U8)(atoi(acBufferTemp));
        memset( acBufferTemp , 0 , sizeof(acBufferTemp) );          /* Reset buffer */
        memcpy( acBufferTemp , (const void*) ( pacLongitude + 3U ) , 2 );
        u8Minutes = (U8)(atoi(acBufferTemp));
        memcpy( acBufferTemp , (const void*) ( pacLongitude + 6U ) , 4 );
        return;
    }
    

    My line of command with result below:

    "c:\Cppcheck\cppcheck.exe"  -DMDF4_TEST_ABG5 --enable=all --inline-suppr --suppress=unusedFunction --suppress=missingInclude --addon=./misra.json ./*.c
    Checking essai_misra_17_7.c ...
    Checking essai_misra_17_7.c: MDF4_TEST_ABG5=1...
    essai_misra_17_7.c:5:11: style: The value returned by a function having non-void return type shall be used [misra-c2012-17.7]
        memset( acBufferTemp , 0 , sizeof(acBufferTemp) );          /* Reset buffer */
    

    memset and memcpy return void pointer.
    They both should generate deviation to Misra 17.7 rule.
    *
    I have checked in dump file and type pointer field is equal to 1. So the python script should generates an error, but doesn't. I don't know why.

     
  • Georgiy Komarov

    Georgiy Komarov - 2020-09-04

    Hello,

    Thanks for the report.

    It seems that problem not in MISRA addon. For some reasons, Cppcheck does not include value type of memcpy function in the dump file.

    Here is a minimal example:

    void fn()
    {
        memcpy(buf, buf2, 3);
        memset(buf, 0, sizeof(buf));
    }
    

    This code produces the following tokens in the dump file:

        <token id="0x110c5b0" file="addons/test/memcpy-memset.c" linenr="3" column="5" str="memcpy" scope="0x1112360" type="name" astParent="0x110cb80"/>
        <token id="0x110cb80" file="addons/test/memcpy-memset.c" linenr="3" column="11" str="(" scope="0x1112360" link="0x110ff00" astOperand1="0x110c5b0" astOperand2="0x110fd10"/>
    
    ...
    
        <token id="0x11102e0" file="addons/test/memcpy-memset.c" linenr="4" column="5" str="memset" scope="0x1112360" type="name" astParent="0x110f510"/>
        <token id="0x110f510" file="addons/test/memcpy-memset.c" linenr="4" column="11" str="(" scope="0x1112360" link="0x110df00" astOperand1="0x11102e0" astOperand2="0x110e8b0" valueType-type="void" valueType-pointer="1" valueType-reference="None" valueType-originalTypeName="void *"/>
    

    The ( token after memcpy does not contain the value type, expected by misra.py here: https://github.com/danmar/cppcheck/blob/1daf1ec108f66633f49c51a778988fe337d6cf13/addons/misra.py#L2162

     
  • Daniel Marjamäki

    thanks georgy! Sounds like an easy fix. update memcpy configuration in cfg/std.cfg file. If this line is added: <returnValue type="void*"/> then the return type should be added.

     
    • Georgiy Komarov

      Georgiy Komarov - 2020-09-05

      Thanks, here is a fix: https://github.com/danmar/cppcheck/pull/2777
      This patch fixes MISRA 17.7 false positives.

       
  • Jean-Loic MARTIN

    Thank you very much for your good job!
    I have implemented the fix and it works well... 58 new MISRA errors in my code !

    Note: It was false negatives.

     
  • Jean-Loic MARTIN

    Hi all,
    i have checked the std.cfg file and it seems that the problem occurs also for the following functions (which have no return type dexcribed):

    void * memmove(void *ct, const void *cs, size_t n); 
    wchar_t * wmemmove(wchar_t *ct, const wchar_t *cs, size_t n);
    

    Is it possible to add the return type for these two functions please ?

     

    Last edit: Jean-Loic MARTIN 2020-09-14
  • Daniel Marjamäki

    do you think you could create a github pull request to fix such problems?

     
  • Jean-Loic MARTIN

    I never used github. I have created an account but don't know how to do a accurate pull request.

     
  • Jean-Loic MARTIN

    Hi , with your file, it gives me an error :

    nofile:0:0: information: Failed to load std.cfg. Your Cppcheck installation is broken, please re-install. The Cppcheck binary was compiled without FILESDIR set. Either the std.cfg should be available in c:/Cppcheck/cfg or the FILESDIR should be configured. [failedToLoadCfg]

     
    • Daniel Marjamäki

      that is a major problem. how do you compile/install? do you run the CLI or GUI?

       
  • Jean-Loic MARTIN

    Hi Daniel, i have installed CppCheck V2.1 and unzip it in C:\CppCheck. That's all.
    Then i call CppCheck directly in pre-build (for naming convention) and post-build rules (for misra rules). I have just downloaded the std.cfg file from https://github.com/danmar/cppcheck/commit/98b62384504f539d06c686fdc5dbaf0cdf14daa7 and copy it in C:\CppCheck/cfg directory. And it gives me this error. I don't know why. I have replaced it with my old std.cfg files and it runs well.

     

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.