Same object modified and read without an intervening sequence point
ID |
c.correctness.sequence_point_violation |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Undefined Behavior |
Language |
C / C++ |
Description
Reports the same variable modified more than once, or modified and read, between two sequence points — an assignment whose target is also incremented on the right-hand side, two increments/decrements of the same variable combined in one expression, or the same variable passed to a call both plain and incremented.
Rationale
The C and C standards leave the order of side effects between sequence points unspecified, and modifying the same object twice (or modifying and reading it) without an intervening sequence point is undefined behavior, not merely unspecified — the compiler is free to evaluate the expression however it likes, reorder it, or assume it never happens. Code like `x = x;` or f(i++, i) can print or store a different result with a different compiler, optimization level, or even a different point in the same binary.
The following code illustrates the pattern detected by this rule:
int x = 0;
void self_assign_postincrement()
{
// FLAGGED: Same object modified and read without an intervening sequence point
x = x++;
}