11-07-2025, 03:37 PM
We agree the description of the example could be better and having 'm' as a parameter of the function just complicates things further.
Our plan is to reword example 'f3' as:
The following example focuses on loops, and the following:
* Since 'j' is only written to through increments, all writes are preceded by a read.
Additionally, since 'j' is observed withing the loop, it is considered observed at
the end of the loop, after the write that happens in the last iteration. So all
write accesses to 'j' are necessary.
* Similarly, for 'k', there is also a read between two writes. However the variable
is never observed, therefore all writes are unnecessary.
* For 'm', the situation is different. The value written to 'm' during the increment
at the end of the first iteration will be overwritten, without having been read, when
assigning 10 to 'm' at the start of the second iteration of the loop (and on all
subsequent iterations). This write is therefore unnecessary. Note: If it were
possible for the loop to have a single iteration, the code would be compliant, because
there would be a feasible path where the unnecessary overwrite does not occur.
int32_t f3()
{
int32_t j = 0;
int32_t k = 0;
int32_t m;
for ( int32_t i = 0; i < 10; ++i ) // Compliant - i is observed in i < 10
{
m = 10; // Compliant - value read during the increment
++k; // Non-compliant - k is never observed
use( j ); // Observation of j inside of the loop
++j; // Compliant - observation above is sufficient for final write</code>
++m; // Non-compliant - observed in the return, but not read before
// being overwritten in the next iteration
} // j is considered observed here as it was observed in the loop
return m;
}
Our plan is to reword example 'f3' as:
The following example focuses on loops, and the following:
* Since 'j' is only written to through increments, all writes are preceded by a read.
Additionally, since 'j' is observed withing the loop, it is considered observed at
the end of the loop, after the write that happens in the last iteration. So all
write accesses to 'j' are necessary.
* Similarly, for 'k', there is also a read between two writes. However the variable
is never observed, therefore all writes are unnecessary.
* For 'm', the situation is different. The value written to 'm' during the increment
at the end of the first iteration will be overwritten, without having been read, when
assigning 10 to 'm' at the start of the second iteration of the loop (and on all
subsequent iterations). This write is therefore unnecessary. Note: If it were
possible for the loop to have a single iteration, the code would be compliant, because
there would be a feasible path where the unnecessary overwrite does not occur.
int32_t f3()
{
int32_t j = 0;
int32_t k = 0;
int32_t m;
for ( int32_t i = 0; i < 10; ++i ) // Compliant - i is observed in i < 10
{
m = 10; // Compliant - value read during the increment
++k; // Non-compliant - k is never observed
use( j ); // Observation of j inside of the loop
++j; // Compliant - observation above is sufficient for final write</code>
++m; // Non-compliant - observed in the return, but not read before
// being overwritten in the next iteration
} // j is considered observed here as it was observed in the loop
return m;
}
Posted by and on behalf of
the MISRA C++ Working Group
the MISRA C++ Working Group