Menu

[MISRAC2012-RULE_9_3 False positive

spoorcc
2023-06-06
2023-06-09
  • spoorcc

    spoorcc - 2023-06-06

    Hi All,

    I see a false positive when initializing an array with pointers to structs, if I use single member struct it there is no Misra report. Also when explicitly using the array indices there is no issue. Using Cppcheck 2.10.

    typedef struct
    {
        int16_t x;
        int16_t y;
    } point_t;
    
    static void MyArray(void)
    {
        const point_t firstPoint = {166, 4};
        const point_t secondPoint = {130, 4};
    
        const point_t *points[2] = {&firstPoint, &secondPoint}; /* false positive style: [misra-c2012-9.3]" */
    }
    
    /* this works ! */
    typedef struct
    {
        int16_t abs2X;
    } singleDimPoint_t;
    
    static void MyArraySingleDimArray(void)
    {
        const singleDimPoint_t firstPoint = {166};
        const singleDimPoint_t secondPoint = {130};
    
        const singleDimPoint_t *points[2] = {&firstPoint, &secondPoint}; /* No issue */
    }
    
    /* and this also works -> a good workaround ? */
    static void MyArray_NamedInitialization(void)
    {
        const point_t firstPoint = {166, 4};
        const point_t secondPoint = {130, 4};
    
        const point_t *points[2] = {[0] = &firstPoint, [1] = &secondPoint}; /* No issue */
    }
    
     
  • Daniel Marjamäki

    Thanks for reporting! I can reproduce with cppcheck-2.10 but not with latest cppcheck HEAD. This might be fixed.

    I want to release cppcheck 2.11 soon. When we release it can you please double check if you can reproduce some such false positive with that?

     
  • spoorcc

    spoorcc - 2023-06-09

    Will recheck with 2.11. If have also a few others I will report if still present in 2.11.

     

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.