XHATN - 2 days ago

I found that cppcheck’s addon dump can produce MISRA diagnostics in a
non-source order.

In SymbolDatabase::printXml() (lib/symboldatabase.cpp), variables are
collected in std::set<const Variable*> before the <variables> XML section
is written. This orders records by object-pointer address instead of a stable
source key.

The MISRA addon reads that XML order into data.variables, and Rule 9.3 then
iterates data.variables directly. Therefore Rule 9.3 diagnostics can be
reported in a different order from the source order, making golden-output
tests flaky.

With the attached test.c, two separate executions using the same cppcheck
version and command produced the same three diagnostics in different orders.

Cppcheck version: 2.21.0
Platform: Windows

Expected diagnostic order:
line 24, line 25, line 27

test.c:24:13: style: Variable 'uniformNormalArray' can be declared as const array 
test.c:25:13: style: Variable 'uniformMatrix' can be declared as const array

test.c:24:13: style: Arrays shall not be partially initialized [misra-c2012-9.3]
    int32_t uniformNormalArray[3] = { INT32_C(0) };
            ^
test.c:25:13: style: Arrays shall not be partially initialized [misra-c2012-9.3]
    int32_t uniformMatrix[2][3] = { { INT32_C(0) } };
            ^
test.c:27:17: style: Arrays shall not be partially initialized [misra-c2012-9.3]
    ArrayHolder uniformArrayHolder = {
                ^

Observed diagnostic order:
line 27, line 25, line 24 ("random" order)

test.c:27:17: style: Arrays shall not be partially initialized [misra-c2012-9.3]
    ArrayHolder uniformArrayHolder = {
                ^
test.c:25:13: style: Arrays shall not be partially initialized [misra-c2012-9.3]
    int32_t uniformMatrix[2][3] = { { INT32_C(0) } };
            ^
test.c:24:13: style: Arrays shall not be partially initialized [misra-c2012-9.3]
    int32_t uniformNormalArray[3] = { INT32_C(0) };
            ^

Suggested fix: serialize addon variables using a stable key, for example
source file + line + column, or variable ID, rather than pointer order.