Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 0.1.1
#2
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;
}
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Messages In This Thread
Rule 0.1.1 - by Merge - 30-06-2025, 12:59 PM
RE: Rule 0.1.1 - by misra cpp - 11-07-2025, 03:37 PM
RE: Rule 0.1.1 - by Merge - 14-07-2025, 08:54 AM
RE: Rule 0.1.1 - by misra cpp - 18-07-2025, 12:00 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)