Menu

cppcheck Misra 17.3 and UINT8_C

2024-12-02
2024-12-02
  • Andrea Innocenti

    Hello,

    I have configured cppcheck2.16.0 in order to parse misra informations.
    It seems to me that everything is working correctly but I have a strange behavior related to the rule in object.

    I am working with arm-none-eabi-gcc, and I am using stdint.h as a source for the types defined as integers in order to guarantee the correct int.

    What I am noticing is the following:

    I have this line of code that triggers the warning:

    if ((size > UINT8_C(0)) && (size <= UINT8_C(7))) {<--- A function shall not be declared implicitly
    

    while the following is accepted:

    uint16_t size = data[0] & UINT8_C(0x0F);
    

    The two lines of code are in the same source file, therefore I can exclude that they are treated in a different way.

    I have different warnings and different "not warning in the same file". What I can think about is that in the first case, the one with the warning, the UINT8_C is inside an if condition, while in the second case, UINT8_C is used in an assignment to a variable.

    This seems always the case except in a couple of occasions like

    switch ((data[0] & UINT8_C(0xF0)) >> UINT8_C(4))
    
     if (minDelayMs >= UINT8_C(0xF1)) {
    

    It seems to me a false positive. C compiler does not issue any warning for the file. Just the misra parser.

    Do you see anything wrong with the use of UINT8_C in those situations?

    Thank you in advance

     
  • Andrea Innocenti

    I would like to add that refactoring the code in this way

      uint8_t seven = UINT8_C(7);
      uint8_t zero = UINT8_C(0);
    if ((size > zero) && (size <= seven))
    

    the warning disappears.
    It seems a false positive to me.

    What do you think?

     
  • Daniel Marjamäki

    Thanks for the bug report. I can reproduce and I agree it's a FP. I have created this ticket:
    https://trac.cppcheck.net/ticket/13370

     

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.