[previous] 2872 [next] This loop, if entered, will never terminate.
Control flow

If this loop is entered it will never terminate.

Consider the two loop constructs shown below.

1) In the first example, the loop is always entered and is always infinite. Message 2871 is generated for this case.

2) In the second example, execution of the loop depends on the value of the function parameter "n". If "n" is greater than 10, the loop will not be entered; but if it is entered, the loop will never terminate. Message 2872 is generated for this case.


/*PRQA S 2017,2467,3120,3204,3227,3408 ++*/

void f1(void)
{
    int i;
    int n = 5;

    for (i = 0; i < n;    )     	/* 2871 */
    {

    }
}

void f2(int n)
{
    while (n <= 10)              	/* 2872 */
    {

    }
}


MISRA-C:2004 Rules applicable to message 2872:

Rule  21.1  (Required) Minimisation of run-time failures shall be ensured by the use of at least one of (a) static analysis tools/techniques; (b) dynamic analysis tools/techniques; (c) explicit coding of checks to handle run-time faults


See also:

QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents