CWE Rule 783
Description
Rule Description
The program uses an expression in which operator precedence causes incorrect logic to be used.
Polyspace Implementation
The rule checker checks for Possibly unintended evaluation of expression because of operator precedence rules.
Examples
Possibly unintended evaluation of expression because of operator precedence rules
This issue occurs when an arithmetic expression result is possibly unintended because operator precedence rules dictate an evaluation order that you do not expect.
The defect highlights expressions of the form x
. Here,
op_1
y
op_2
zop_1
and op_2
are operator
combinations that commonly induce this error. For instance, x == y |
z
.
The checker does not flag all operator combinations. For instance, x == y ||
z
is not flagged because you most likely intended to perform a logical OR
between x == y
and z
. Specifically, the checker
flags these combinations:
&&
and||
: For instance,x || y && z
orx && y || z
.Assignment and bitwise operations: For instance,
x = y | z
.Assignment and comparison operations: For instance,
x = y != z
orx = y > z
.Comparison operations: For instance,
x > y > z
(except when one of the comparisons is an equalityx == y > z
).Shift and numerical operation: For instance,
x << y + 2
.Pointer dereference and arithmetic: For instance,
*p++
.
The defect can cause the following issues:
If you or another code reviewer reviews the code, the intended order of evaluation is not immediately clear.
It is possible that the result of the evaluation does not meet your expectations. For instance:
In the operation
*p++
, it is possible that you expect the dereferenced value to be incremented. However, the pointerp
is incremented before the dereference.In the operation
(x == y | z)
, it is possible that you expectx
to be compared withy | z
. However, the==
operation happens before the|
operation.
See if the order of evaluation is what you intend. If not, apply parentheses to implement the evaluation order that you want.
For better readability of your code, it is good practice to apply parenthesis to implement an evaluation order even when operator precedence rules impose that order.
int test(int a, int b, int c) { return(a & b == c); //Noncompliant }
In this example, the ==
operation happens
first, followed by the &
operation. If you
intended the reverse order of operations, the result is not what you
expect.
One possible correction is to apply parenthesis to implement the intended evaluation order.
int test(int a, int b, int c) { return((a & b) == c); }
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)