Menu

False negative with MISRA 13.5 rule

2020-12-04
2020-12-05
  • Jean-Loic MARTIN

    Hi all,
    I think there is a problem with Misra Add-on and rule 13.5:
    "Rule 13.5 The right hand operand of a logical && or || operator shall not contain persistent side effects"

    My code sample:

    #define FORCE_LOG_TIMEOUT 60U
    #define LOW_LEVEL_FREE_SPACE 1000U
    #define VERY_LOW_LEVEL_FREE_SPACE 50U
    
    extern unsigned int u32LastTimeForceLogSpaceLowLevel;
    extern unsigned char bMaster;
    extern unsigned int TaskLog_u32GetFreeSpace(void);
    extern unsigned int HAL_GetTick(void);
    
    static unsigned char bForceLogLowLevel( void )
    {
        unsigned char bReturn = 0u;
    
        if ( bMaster != 0u )
        {
            u32LastTimeForceLogSpaceLowLevel = HAL_GetTick();
        }
        else
        {
            u32LastTimeForceLogSpaceLowLevel = 0xFFFFFFFFU;
        }
    
        if ( (unsigned int)( HAL_GetTick() - u32LastTimeForceLogSpaceLowLevel ) < FORCE_LOG_TIMEOUT )
        {
           bReturn = 1u;
        }
    
        return bReturn;
    }
    
    
    unsigned int PushLogData( void )
    {
        unsigned int u32SDCardFreeSpace = TaskLog_u32GetFreeSpace();
    
        if ( ( u32SDCardFreeSpace > LOW_LEVEL_FREE_SPACE ) ||
             ( ( u32SDCardFreeSpace > VERY_LOW_LEVEL_FREE_SPACE ) && ( bForceLogLowLevel() == 1u ) )   /* This violate 13.5 MISRA C:2012 rule*/
           )                                                                         /* bForceLogLowLevel function has persistent */
        {                                                                            /* side effect, because it modifies a global variable */
            return (u32SDCardFreeSpace*10u);                                         /* u32LastTimeForceLogSpaceLowLevel */
        }
        return 0u;
    }
    

    As you can see , a small function PushLogData contains an if statement.
    This if statement contains a right hand operand which call a function bForceLogLowLevel. This function modifies a global variable u32LastTimeForceLogSpaceLowLevel. This is considered has a persistent side effect (modify object).

    When i look at misra python script rules 13.5, it calls hasSideEffectsRecursive.
    In this "function" there is a: # Todo: Check function calls

    So i don't know if this rules (13.5) is really available, because side effects will occurs in functions call.

    Next thing i use release cppcheck 2.2 without any modification and with above code, the add-on doesn't indicate any failure.

    Thanks !

     

    Last edit: Jean-Loic MARTIN 2020-12-04
  • Daniel Marjamäki

    hmm.. yes we should not claim that 13.5 is completed yet.

     
  • Jean-Loic MARTIN

    Hi Daniel,
    Thanks for your quick answer !
    Unfortunately, i'm clearly not an expert in Python and can't help for this. So i will wait.
    Regarding this rules, verifying that a function doesn't have any side effect seems very difficult to do !

     
  • Daniel Marjamäki

    Regarding this rules, verifying that a function doesn't have any side effect seems very difficult to do !

    Well in this case.. I think we should at least make some limited analysis and if we are uncertain after that then assume there is side effects.

    To definitely determine if arbitrary function calls have side effects ; that would be extremely difficult.

     
  • Georgiy Komarov

    Georgiy Komarov - 2020-12-05

    Regarding this rules, verifying that a function doesn't have any side effect seems very difficult to do !

    This is a difficult problem. When we run the addon, we only have access to one Cppcheck dump file. This means that we can only analyze functions defined in the current file.

     
  • Daniel Marjamäki

    that is out of scope for this check imho. if the function body is not seen in the dump file we cannot analyze it and then we fallback and assume it has side effects.

     

    Last edit: Daniel Marjamäki 2020-12-05

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.