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++)
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.
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; }
Select the red Non-terminating loop result. The Source pane highlights the
for
loop inmain
.Trace from the
for
loop inmain
to the operation causing the error. The operation isx+= a[10 + (i*j)]
. An Out of bounds array index error occurs wheni
is 9 andj
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 redfor
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 functionf
. Insidef
, 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.