CWE Rule 835
Description
Loop with Unreachable Exit Condition
Polyspace Implementation
The rule checker checks for Infinite loop.
Examples
This issue occurs when a loop termination condition is never satisfied after the loop is entered. This can happen due to improper validation or update of loop variables, or if external factors prevent the loop from reaching its terminating state.
Polyspace does not report a violation on intentional infinite loops such as
while(1). Code following intentional infinite loops are flagged by
the Unreachable code checker.
Unintended infinite loops often indicate an error in the program logic. For instance, these programming errors can lead to unintended infinite loops:
The loop index is never updated inside the loop.
The loop index is updated in branches inside the loop that are unreachable.
The loop index is updated in a way that the index never satisfies the loop termination condition.
Fix the programming error, if any. Make sure that the code that updates the loop index is reachable and that the loop index eventually acquires a value that makes the loop terminate.
If you determine that the loop termination condition is satisfied under certain circumstances (false positive), add comments to your result or code to avoid another review.
In this example, the loop uses two indices to cycle through two arrays. The index used in the loop termination condition is never updated inside the loop, possibly because of a programming error (the other index is updated twice).
void cleanArray(int* fullArray, int* finalArray, int lenFullArray, int lenfinalArray) {
int i,j;
for(i = 0, j = 0; i < lenFullArray; j++) { // Noncompliant
if(fullArray[i] >= 0 && j < lenfinalArray) {
finalArray[j] = fullArray[i];
j++;
}
}
}Make sure to update the loop index used in the loop termination condition.
void cleanArray(int* fullArray, int* finalArray, int lenFullArray, int lenfinalArray) {
int i,j;
for(i = 0, j = 0; i < lenFullArray; i++) { // Compliant
if(fullArray[i] >= 0 && j < lenfinalArray) {
finalArray[j] = fullArray[i];
j++;
}
}
}Check Information
| Category: Behavioral Problems |
PQL Name:
std.cwe_native.R835 |
Version History
Introduced in R2026a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)