Menu

cppcheck 2.7 - Variable-length array violation reported with rule 8.2 warning instead of 18.8

2024-05-03
2024-05-13
  • Roberto Geografo

    Hi,

    In the following example, cppcheck 2.7 produces a warning for violating MISRA rule 8.2 on line 3:

    #include <stddef.h>
    
    static void example_func(size_t num, int param1[num]);
    
    int main(void)
    {
        int var[] = {1, 2};
        example_func(2, var);
        return 0;
    }
    
    static void example_func(size_t num, int param1[num])
    {
        (void)param1; // unused
    }
    

    The command launched is the following:

    cppcheck --addon=misra.py main.c --suppress=misra-c2012-2.7
    

    And the output is:

    Checking main.c ...
    main.c:3:25: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-8.2]
    static void example_func(size_t num, int param1[num]);
                            ^
    

    However, if I instead, define a constant as in the code below, the rule 8.2 violation disappears:

    #include <stddef.h>
    #define NUM 2
    
    static void example_func(size_t num, int param1[NUM]);
    
    int main(void)
    {
        int var[] = {1, 2};
        example_func(2, var);
        return 0;
    }
    
    static void example_func(size_t num, int param1[NUM])
    {
        (void)param1; // unused
    }
    

    Should it be reported as rule 18.8 violation instead?

    Thanks

     

    Last edit: Roberto Geografo 2024-05-07
  • Oleksandr Labetskyi

    Hello, Roberto!
    Yes, it's should be reported as 18.8. We've created a ticket for your issue.

     

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.