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.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)