Hi, I found a false positive regarding the rule uninitvar.
In the following C++ program, cppcheck reports that array a is uninitialized when a[0] is returned. However, a[0] is always initialized before the return statement.
The loop executes with i == 0, i == 1, and i == 2. For i == 0 and i == 1, the guard i >= 0 && i < 2 is true, so both a[0] and a[1] are assigned. The final iteration with i == 2 skips the write, but it does not make a[0] uninitialized.
Actual result
Cppcheck reports:
<errorid="uninitvar"severity="warning"msg="Uninitialized variable: a"...><locationline="8"column="10"/><locationline="5"column="16"info="Assuming condition is false"/><symbol>a</symbol></error>
Cppcheck also reports knownConditionTrueFalse for i >= 0, which is expected and not part of this issue.
Expected result
Cppcheck should not report uninitvar for a at return a[0].
Hi, I found a false positive regarding the rule
uninitvar.In the following C++ program, cppcheck reports that array
ais uninitialized whena[0]is returned. However,a[0]is always initialized before the return statement.The loop executes with
i == 0,i == 1, andi == 2. Fori == 0andi == 1, the guardi >= 0 && i < 2is true, so botha[0]anda[1]are assigned. The final iteration withi == 2skips the write, but it does not makea[0]uninitialized.Actual result
Cppcheck reports:
Cppcheck also reports
knownConditionTrueFalsefori >= 0, which is expected and not part of this issue.Expected result
Cppcheck should not report
uninitvarforaatreturn a[0].Verification
The issue was reproduced with:
Version: 2.21.0
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14957