CWE Rule 483
Description
Rule Description
The code does not explicitly delimit a block that is intended to contain 2 or more statements, creating a logic error.
Polyspace Implementation
The rule checker checks for these issues:
Incorrectly indented statement
Semicolon on the same line as an if, for or while statement
Examples
This issue occurs when the indentation of a
statement makes it appear as part of an if, else or
another block but the arrangement or lack of braces actually keeps the statement outside the
block.
A developer or reviewer might incorrectly associate the statement with a block based on its indentation, leading to an incorrect assumption about the program logic.
For instance, in this example:
if(credentialsOK())
login=1;
setCookies();setCookies(); is not part of the if block, but the indentation suggests otherwise.If you want a statement to be part of a block, make sure that the statement is within the braces associated with the block. To identify the extent of a block, on the Source pane, click the opening brace.
If an if, else or while
statement has no braces following the condition, only the next line on an execution path up
to a semicolon is considered part of the if, else or
while block. If you want subsequent lines to be included in the block,
wrap the lines in braces.
For instance, in the preceding example, to include both statements in the if block, use:
if(credentialsOK()) {
login=1;
setCookies();
}int switch1, switch2;
void doSomething(void);
void doSomethingElse(void);
void func() {
if(switch1)
if(switch2)
doSomething();
else //Noncompliant
doSomethingElse();
}In this example, the else is indented as if it is associated with the first
if. However, the else is actually associated with
the second if. The indentation does not match the actual association and
might lead to incorrect assumptions about the program logic.
If you want the else to be associated with the first if, use braces to mark the boundaries of the first if block.
int switch1, switch2;
void doSomething(void);
void doSomethingElse(void);
void func() {
if(switch1) {
if(switch2)
doSomething();
}
else
doSomethingElse();
}This issue occurs when a semicolon on the same line as the last token of an
if, for or while statement results
in an empty body.
The checker makes an exception for the case where the if statement is immediately followed by an else statement:
if(condition);
else {
...
}The semicolon following the if, for or while statement often indicates a programming error. The spurious semicolon changes the execution flow and leads to unintended results.
If you want an empty body for the if, for or while statement, wrap the semicolon in a block and place the block on a new line to explicitly indicate your intent:
if(condition)
{;}int credentialsOK(void);
void login () {
int loggedIn = 0;
if(credentialsOK()); //Noncompliant
loggedIn = 1; //Noncompliant
}In this example, the spurious semicolon results in an empty if body. The assignment loggedIn=1 is always performed. However, the assignment was probably to be performed only under a condition.
If the semicolon was unintended, remove the semicolon.
int credentialsOK(void);
void login () {
int loggedIn = 0;
if(credentialsOK())
loggedIn = 1;
}Check Information
| Category: Behavioral Problems |
Version History
Introduced in R2023a
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.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- 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)