Main Content

CERT C: Rec. EXP00-C

Use parentheses for precedence of operation

Description

Rule Definition

Use parentheses for precedence of operation.1

Polyspace Implementation

The rule checker checks for Possibly unintended evaluation of expression because of operator precedence rules.

Examples

expand all

Issue

Possibly unintended evaluation of expression because of operator precedence rules 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 op_1 y op_2 z. Here, op_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 or x && y || z.

  • Assignment and bitwise operations: For instance, x = y | z.

  • Assignment and comparison operations: For instance, x = y != z or x = y > z.

  • Comparison operations: For instance, x > y > z (except when one of the comparisons is an equality x == y > z).

  • Shift and numerical operation: For instance, x << y + 2.

  • Pointer dereference and arithmetic: For instance, *p++.

Risk

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 pointer p is incremented before the dereference.

    • In the operation (x == y | z), it is possible that you expect x to be compared with y | z. However, the == operation happens before the | operation.

Fix

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.

Example - Expressions with Possibly Unintended Evaluation 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.

Correction — Parenthesis For Intended Order

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

Group: Rec. 03. Expressions (EXP)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.