CWE-783: Operator Precedence Logic Error

Description

The product uses an expression in which operator precedence causes incorrect logic to be used.

Submission Date :

July 16, 2009, midnight

Modification Date :

2023-06-29 00:00:00+00:00

Organization :

MITRE
Extended Description

While often just a bug, operator precedence logic errors can have serious consequences if they are used in security-critical code, such as making an authentication decision.

Example Vulnerable Codes

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.



// // call method to authenticate username and password// 
// // if authentication fails then return failure otherwise return success// 
return isUser;
isUser = SUCCESS;
int isUser = FAIL;if (isUser = AuthenticateUser(username, password) == FAIL) {}else {}return isUser;#define FAIL 0#define SUCCESS 1...int validateUser(char *username, char *password) {}

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.


...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.


// // calculate return on investment// 
double returnROI = 0.0;returnROI = currentValue - initialInvestment / initialInvestment;return returnROI;public double calculateReturnOnInvestment(double currentValue, double initialInvestment) {}

However, the return on investment calculation will not produce correct results because of the incorrect operator precedence logic in the equation. The divide operator has a higher precedence than the minus operator, therefore the equation will divide the initial investment costs by the initial investment costs which will only subtract one from the current value. Again this operator precedence logic error can be resolved by the correct use of parentheses within the equation, as shown below.


...returnROI = (currentValue - initialInvestment) / initialInvestment;...

Note that the initialInvestment variable in this example should be validated to ensure that it is greater than zero to avoid a potential divide by zero error (CWE-369).

Related Weaknesses

This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.

Visit http://cwe.mitre.org/ for more details.