A C standard library name is redefined or shadowed
ID |
objectivec.maintainability.naming_stdlib_name_reuse |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Naming |
Language |
Objective-C |
Description
Reports a #define, a C function definition, or a variable declaration that reuses the name of a well-known C standard library function - malloc, free, strlen, printf, qsort and the rest of the memory, string, formatting, conversion and process families. Calls to those functions are not reported, only definitions and declarations that take the name. Everyday English words that merely happen to name a library function, such as time and index, are deliberately left out so the rule stays quiet.
Rationale
Objective-C is C, so every translation unit already has these names declared by the headers it includes, and the standard reserves them for the implementation. A #define that takes one of them rewrites every later call in the translation unit, including calls made from inside a system header that was included afterwards - which is how a tracking macro over malloc ends up rewriting an allocation inside Foundation. A function or variable with the same name is quieter but no better: the reader who sees free(buffer) two hundred lines later has no reason to suspect that free is not the one from stdlib.h, and whether the local or the library version is called depends on include order and linkage.
The following code illustrates the pattern detected by this rule:
// FLAGGED: A C standard library name is redefined or shadowed
#define malloc(sz) xy_tracked_malloc(sz)
Remediation
Give the definition a name of its own, with the project’s class prefix - XYMalloc, xy_strdup, freeSlots. Where the intent really is to instrument the library function, interpose at the linker instead of redefining the name, so the substitution is explicit and confined to the target it was meant for.