Array parameter declared with a static minimum size
ID |
objectivec.maintainability.static_array_param_size |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Api Design |
Language |
Objective-C |
Description
Reports an array parameter written with the C99 static size qualifier - float curve[static 16] - which declares that the caller always passes at least that many elements.
Rationale
The qualifier reads like a bounds check and is not one. It is a promise from the caller to the compiler, and nothing on either side has to verify it: passing a shorter array, or nil, is undefined behaviour that most compilers accept without a word, while the optimiser is now free to assume the pointer is non-null and to prefetch elements the caller never supplied. The syntax is also unfamiliar enough that a reader is likely to take it for a static storage specifier, and it decays to a plain pointer at the call site anyway, so the length it names is not available where the code actually indexes.
The following code illustrates the pattern detected by this rule:
// FLAGGED: Array parameter declared with a `static` minimum size
static void normalise(float coefficients[static 4], float gain) {
for (int i = 0; i < 4; i++) {
coefficients[i] *= gain;
}
}