...
These two requirements can be counter intuitive when expressions contain side-effects. This is because such expressions follow the left to right evaluation order irrespective of operator precedence, associativity rules and indicative parentheses. The best practice is to avoid using expressions containing multiple side-effects. When used, the expressions must be carefully structured to respect the left to right evaluation order.
Noncompliant Code Example
This noncompliant code example shows how side-effects in expressions can lead to unanticipated outcomes. The programmer intends to write access control logic based on different threshold levels. Each user has a rating that must be above the threshold to be granted access. As shown, a simple function can be used to calculate the rating. The get()
method is expected to return a non-zero factor when the user is authorized, and a zero value when not.
...
Code Block | ||
---|---|---|
| ||
class BadPrecedence { public static void main(String[] args) { int number = 17; int[] threshold = new int[20]; threshold[0] = 10; number = (number > threshold[0]? 0 : -2) + ((31 * ++number) * (number = get())); // ... if(number == 0) { System.out.println("Access granted"); } else { System.out.println("Denied access"); // number = -2 } } public static int get() { int number = 0; // Assign number to non zero value if authorized else 0 return number; } } |
Compliant Solution
By diligently following the left to right evaluation order, a programmer can expect this compliant solution to evaluate to the expected final outcome depending on the value returned by the get()
method.
...
Although this solution solves the problem, in general it is advisable to avoid using expressions with more than one side-effect. It is also inadvisable to depend on the left-right ordering for evaluation of side-effects because operands are evaluated in place first, and then subject to laws of operator precedence and associativity.
Compliant Solution
This compliant solution uses an equivalent expression with no side-effects. This allows the expression to be reordered without concern for the evaluation order of the component expressions, making the code easier to understand and maintain.
Code Block | ||
---|---|---|
| ||
number = ((31 * (number + 1)) * get()) + (get() > threshold[0]? 0 : -2); |
Risk Assessment
Failing to keep in mind the evaluation order of expressions containing side effects can result in unexpected output.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP30- J | low | unlikely | medium | P2 | L3 |
Other Languages
This rule appears in the C Coding Standard as EXP30-C. Do not depend on order of evaluation between sequence points.
This rule appears in the C++ Coding Standard as EXP30-CPP. Do not depend on order of evaluation between sequence points.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Wiki Markup |
---|
\[[JLS 05|AA. Java References#JLS 05]\] Section 15.7 "Evaluation Order" and 15.7.3 "Evaluation Respects Parentheses and Precedence" |
...