Increment or decrement inside a sizeof operand never executes
ID |
c.correctness.side_effect_in_sizeof |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
C / C++ |
Description
This operand of sizeof contains an increment or decrement. Except when the operand is a variable-length array, sizeof never evaluates its operand at run time — it only inspects the operand’s type — so this side effect never happens and the variable is never actually incremented or decremented. Move the increment/decrement out of the sizeof expression as its own statement.
Rationale
This operand of sizeof contains an increment or decrement. Except when the operand is a variable-length array, sizeof never evaluates its operand at run time — it only inspects the operand’s type — so this side effect never happens and the variable is never actually incremented or decremented. Move the increment/decrement out of the sizeof expression as its own statement.
The following code illustrates the pattern detected by this rule:
void post_increment(int x)
{
// FLAGGED: Increment or decrement inside a sizeof operand never executes
size_t s = sizeof(x++);
(void)s;
}
Remediation
Follow secure coding practices and review the references below for detailed remediation guidance.