sizeof applied to the result of another sizeof

ID

objectivec.correctness.sizeof_sizeof

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Suspicious Construct

Language

Objective-C

Description

Reports sizeof applied directly to another sizeof, in either spelling - sizeof(sizeof x) and the parenthesis-free sizeof sizeof x - including when the two are split across lines. Two independent sizeof operators in one expression, such as sizeof(a) + sizeof(b), are not reported, and neither are occurrences inside a comment or a string literal.

Rationale

sizeof always yields a size_t, so the outer operator measures size_t itself and the whole expression collapses to a constant - 8 on a 64-bit target - with no relation to the type that was written. It is almost always a stray operator left behind while editing an expression, or a parenthesis in the wrong place, and because the result is a plausible small number it survives a casual read and often a test as well. Where the value is used to size an allocation, it silently allocates the wrong amount.

The following code illustrates the pattern detected by this rule:

@implementation Buffers
- (void)doubled {
    // FLAGGED: sizeof applied to the result of another sizeof
    size_t parenthesised = sizeof(sizeof(NSInteger));
    NSInteger value = 0;
    // FLAGGED: sizeof applied to the result of another sizeof
    size_t bare = sizeof sizeof value;
    // FLAGGED: sizeof applied to the result of another sizeof
    size_t wrapped = sizeof(
        sizeof(NSInteger));
    NSLog(@"%zu %zu %zu", parenthesised, bare, wrapped);
}
@end

Remediation

Remove the outer sizeof and keep the inner one. If the parentheses were the problem - sizeof(sizeof(a) * n) written where sizeof(a) * n was meant - fix the grouping instead of the operator.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved