Main Content

Identify Loop Operation with Run-Time Error

This tutorial shows how to interpret Polyspace® Code Prover™ results that highlight a run-time error inside a loop.

If an error occurs in a loop, the error shows in the analysis results as a red Non-terminating loop check on the loop keyword (for, while, and so on).

for (i = 0; i <= 10; i++) 
The operation causing the error shows as an orange check in the loop. To distinguish this orange check from other orange checks in the loop, navigate directly from the red loop keyword to the operation responsible for the run-time error.

In this tutorial, a function is called in a loop. The function body contains another loop, which has an operation causing a run-time error. You trace from the first loop to the operation causing the run-time error.

  1. Run verification on this code and open the results:

    int a[100];
    
    int f (int i);
    
    void main() {
      int i,x=0;
      for (i = 0; i <= 10; i++) {
        x += f(i);
      }
    }
    
    int f (int i) {
      int j, x;
      x = 0;
      for (j = 0; j <= 10; j++) {
        x += a[10 + (i * j)];
      }
      return x;
    }

  2. Select the red Non-terminating loop result. The Source pane highlights the for loop in main.

  3. Trace from the for loop in main to the operation causing the error. The operation is x+= a[10 + (i*j)]. An Out of bounds array index error occurs when i is 9 and j is 10. The error shows in orange on the [ operator.

    To trace from the red for keyword to the orange array access operation:

    • Navigate directly to the operation. Right-click the for keyword and select Go to Cause.

    • See the full history from the for keyword to the array access operation. Select the red for keyword. The Result Details pane shows the history.

      You can read the event history in sequence. The outer loop runs nine times without error. On the tenth iteration (i=9), the loop enters the function f. Inside f, the inner loop runs ten times without error. On the eleventh iteration (j=10), the array access causes an error.

    You can use this information to determine how to fix the run-time error on the array access operation.

Note

You can navigate directly to the root cause of an error for loops with only a small number of iterations.

See Also

Related Examples

More About