-3

I've a function which takes seconds as input and convert it into into time and date. However I'm experiencing warning about Array Variable 'cumDaysInMonths' may not have been initialized [MISRA 2012 Rule 9.1, mandatory]. I've initialized it and then updated in a loop. The warnings are on both lines and linked.

The initialization is as follows:

unsigned short cumDaysInMonths[MONTHS_IN_A_YEAR] = {0,31,59,90,120,151,181,212,243,273,304,334};

And the usage within loop is as follows:

for(list_index = 2; list_index < MONTHS_IN_A_YEAR; list_index++)
{
    cumDaysInMonths[list_index]++;
}

I'm not able to understand the reason of this warning and how to fix it?

enigma
  • 1
  • 1
  • 1
    Please provide a full example which shows the actual warning. Both your snippets together are fine if you place them in the right order. – BluesSolo Oct 31 '18 at 10:19
  • What is MISRA 2012 Rule 9.1, mandatory? – Matthieu Brucher Oct 31 '18 at 10:20
  • 4
    This code is far from a [mcve]. Missing is `MONTHS_IN_A_YEAR` and the declaration of `list_index`. We also have no context as to where, when, or how these two pieces of code are situated in your program. You could have a global array, and then a local array with the same name (by mistake). Post a complete program, not two disjointed snippets. – PaulMcKenzie Oct 31 '18 at 10:23
  • @MatthieuBrucher MISRA is a coding standard. 9.1 wants initialized variables. – Swordfish Oct 31 '18 at 10:23
  • 1
    @AreebTariq: Is this warning given by a static analyser? – P.W Oct 31 '18 at 10:26
  • Remove the quantity when defining an array; let the compiler determine the size. – Thomas Matthews Oct 31 '18 at 14:31

1 Answers1

0

This is most probably a bug in your static analyser which detects a false positive on both lines you have mentioned.
This is a common problem in static analysis and there is even an IEEE paper on elimination of such false positives.

In the Related questions on the right side, I can see at least one question related to a MISRA C++ false positive warning.

P.W
  • 26,289
  • 6
  • 39
  • 76