Menu

False positive misra-c2012-9.3

2023-05-03
2023-05-05
  • Emiel Estiévenart

    Hi

    On a lot of arrays, I get this warning:
    warning GC59630F4: c2012-9.3 MISRA: Required Arrays shall not be partially initialized [misra-c2012-9.3]
    Here's an example of such an array:

    static t_sensor const * apAllFastHspSensors[] =
    {
    // #BEGIN_SENSORS
    /DEFINE_LIST_CAST (t_sensor const)((void const)/
    (t_sensor const)((void const)&entry1),
    (t_sensor const)((void const)&entry2),
    (t_sensor const)((void const)&entry3),
    (t_sensor const)((void const)&entry4),
    (t_sensor const)((void const)&entry5),
    // #END_SENSORS
    (t_sensor const *)(NULL),
    };

    t_sensor is a typedef of a struct.

    I checked by removing the casting and it doesn't change much.
    Any advice?

    Thanks!

     
  • Daniel Marjamäki

    It might be false positives. However I can't say for sure I fail to reproduce. A small example code that would reproduce the warning would be appreciated.

     
  • Emiel Estiévenart

    Hi,

    I was able to reproduce it by creating a typedef of a self referencing struct in a seperate header.
    Creating an array of this type causes the error to come up.
    When I comment out the reference to self, the error goes away.

    How would I fix this?

    #ifndef MY_HEADER_H
    #define MY_HEADER_H
    
    #include <stdint.h>
    
    typedef struct my_struct3 my_type3_t;
    
    struct my_struct3
    {
        my_type3_t* next;
        uint8_t temp;
    };
    
    #endif //MY_HEADER_H
    
     
  • Emiel Estiévenart

    This code in a different source file then throws the error:

    static my_type3_t const some_instance =
    {
        NULL, 
        0
    };
    
    static my_type3_t const* apAllFastHspSensors[] =
    {
        (&some_instance),
        (&some_instance),
        (&some_instance),
        (&some_instance),
        (&some_instance),
        NULL,
    };
    
     
  • Daniel Marjamäki

    Thank you. I can reproduce. I believe this is a false positive. In my opinion your code is compliant. I have created this ticket:
    https://trac.cppcheck.net/ticket/11707

     
  • Emiel Estiévenart

    Thanks :)

     
  • Emiel Estiévenart

    Awesome!
    It didn't work at first, but after I removed the cppcheck build output for the offending file the error was gone. Great support, thanks!

     

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.