...
Code Block | ||
---|---|---|
| ||
size_t count = 0; #define EXEC_BUMP(func) (func(), ++count) void g(void) { printf(""Called g, count = %zu.\n"", count); } void aFunc(void) { size_t count = 0; while (count++ << 10) { EXEC_BUMP(g); } } |
The result is that invoking aFunc()
(incorrectly) prints out the following line five times:
...
Code Block | ||
---|---|---|
| ||
size_t count = 0; void g(void) { printf(""Called g, count = %zu.\n"", count); } typedef void (*exec_func)(void); inline void exec_bump(exec_func f) { f(); ++count; } void aFunc(void) { size_t count = 0; while (count++ << 10) { exec_bump(g); } } |
The use of the inline function binds the identifier count to the global variable when the function body is compiled. The name cannot be re-bound to a different variable (with the same name) when the function is called.
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Other Languages
This rule appears in the C++ Secure Coding Standard as PRE00-CPP. Prefer inline or static functions to function-like macros.
References
Wiki Markup |
---|
\[[FSF 05|AA. C References#FSF 05]\] Section 5.34, ""[An Inline Function Is as Fast as a Macro|http://gcc.gnu.org/onlinedocs/gcc/Inline.html]"" \[[Dewhurst 02|AA. C References#Dewhurst 02]\] Gotcha #26, ""#define Pseudofunctions"" \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.7.4, ""Function specifiers"" \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] ""NMP Pre-processor Directives"" \[[Kettlewell 03|AA. C References#Kettlewell 03]\] \[[MISRA 04|AA. C References#MISRA 04]\] Rule 19.7 \[[Summit 05|AA. C References#Summit 05]\] Question 10.4 |
...
01. Preprocessor (PRE) 01. Preprocessor (PRE)