1651
Scope Impact Likelihood
Confidentiality
Integrity
Availability
Varies by Context
Unexpected State
The consequences will vary based on the context
surrounding the incorrect precedence. In a security
decision, integrity or confidentiality are the most likely
results. Otherwise, a crash may occur due to the software
reaching an unexpected state.
Potential Mitigations
Phase: Implementation
Regularly wrap sub-expressions in parentheses, especially in security-critical code.
Demonstrative Examples
Example 1:
In the following example, the method validateUser makes a call to another method to authenticate
a username and password for a user and returns a success or failure code.
Example Language: C (Bad)
#define FAIL 0
#define SUCCESS 1
...
int validateUser(char *username, char *password) {
int isUser = FAIL;
// call method to authenticate username and password
// if authentication fails then return failure otherwise return success
if (isUser = AuthenticateUser(username, password) == FAIL) {
return isUser;
}
else {
isUser = SUCCESS;
}
return isUser;
}
However, the method that authenticates the username and password is called within an if
statement with incorrect operator precedence logic. Because the comparison operator "==" has a
higher precedence than the assignment operator "=", the comparison operator will be evaluated
first and if the method returns FAIL then the comparison will be true, the return variable will be set
to true and SUCCESS will be returned. This operator precedence logic error can be easily resolved
by properly using parentheses within the expression of the if statement, as shown below.
Example Language: C (Good)
...
if ((isUser = AuthenticateUser(username, password)) == FAIL) {
...
Example 2:
In this example, the method calculates the return on investment for an accounting/financial
application. The return on investment is calculated by subtracting the initial investment costs from
the current value and then dividing by the initial investment costs.
Example Language: Java (Bad)
public double calculateReturnOnInvestment(double currentValue, double initialInvestment) {
double returnROI = 0.0;
// calculate return on investment
CWE Version 4.14
CWE-783: Operator Precedence Logic Error
CWE-783: Operator Precedence Logic Error