Menu

MISRA 4.1 false positive.

2019-07-12
2019-10-04
  • Richard Smith

    Richard Smith - 2019-07-12

    The following addition generates false positive in MISRA rule 4.1

    const char *s41_2 = "\x41\x42";
     int c41_3         = '\141t'; // 4.1
     int c41_4         = '\141\t';
    +int c41_5         = '\0';
    
     
    • versat

      versat - 2019-09-24

      I can reproduce it and this clearly should not be, so i created ticket https://trac.cppcheck.net/ticket/9370

       
  • Richard Smith

    Richard Smith - 2019-07-12

    But interestingly it seems to pass if I use 'python -m pytest addons/test/test-misra.py' instead of calling it directly.

     
  • Mathias Schmid

    Mathias Schmid - 2019-09-23

    The checker for MISRA rule 4.1 works fine for compliant and non-compliant examples part of MISRA 2012 document. However if the hexadecimal and octal escape sequences are not of full length (\x + 2 hex digits or \ + 3 octal digits) than the checker generates false positives. The following lines generate false positives but from my point of view they are compliant. The last one leads to an out of range exception and terminates the MISRA python addon.

    const char *s41_3 = "\x41\xA";
    const char *s41_4 = "\x8" "g";
    int c41_5         = '\0';
    int c41_6         = '\0\t';
    int c41_7         = '\12\t';
    int c41_8         = '\12';
    
     
    • versat

      versat - 2019-09-24

      Do you mean

      const char * c41_6         = "\0\t";
      const char * c41_7         = "\12\t";
      

      instead of

      int c41_6         = '\0\t';
      int c41_7         = '\12\t';
      

      ?
      Otherwise the multi-character character constant seems to have implementation defined behaviour and is not forbidden in C. So this could also be used as an example throwing some warning (maybe not 4.1).

       
  • Mathias Schmid

    Mathias Schmid - 2019-09-24

    I mean

    int c41_6         = '\0\t';
    int c41_7         = '\12\t';
    

    MISRA rule 4.1 states int c41_4 = '\141\t'; as compliant and the above lines just use less than 3 octal digits. The checker state them as non-compliant. It's the same as for the initial comment: int c41_5 = '\0'; uses just less octal digits but should not generate a MISRA 4.1 violation. Hope this sounds plausible ?

     
    • versat

      versat - 2019-09-24

      Yes, i see. MISRA uses similar examples, so that makes sense.

       
  • Georgiy Komarov

    Georgiy Komarov - 2019-09-25

    Here is PR that will fix rule 4.1.
    @mschmid @whoopsmith could you please check that version of misra.py with yours codebase?

     
    • Richard Smith

      Richard Smith - 2019-09-25

      Thank. I'll try to test it later on today.

       
      • Richard Smith

        Richard Smith - 2019-09-25

        See my note on the PR. It now throws errors for stuff like this:

        static const char * mfg_pass = "pass";

         
  • versat

    versat - 2019-10-04

    The PR has been merged (Thanks @Georgy Komarov): https://github.com/danmar/cppcheck/commit/846f356db40ecd29f52c7ad119d6a75db37ddd13
    Verification of MISRA rule 4.1 should work much better now.
    If there are still issues, please report them.

     

Log in to post a comment.