Prefer type definitions (typedef
) to macro definitions (#define
) when encoding types. Type definitions obey scope rules; macro definitions do not. textual substitution is inferior to using the type system, and . While type definitions for non-pointer types have similar advantages [Summit 2005], can make it more difficult to write const
-correct code (see DCL05-C. Use typedefs of non-pointer types only).
Noncompliant Code Example
In this noncompliant code example, uc_a is declared as char *
, but s2
is declared as a char
, which is probably not what the programmer intended:
#define uchar unsigned char uchar uc_a, uc_b;
This code example also violates DCL04-C. Do not declare more than one variable per declaration.
Compliant Solution
In this compliant solution, both s1
and s2
are declared as char *
:
typedef unsigned char uchar;
Risk Assessment
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
PRE03-C | Low | Unlikely | Medium | P2 | L3 |
Automated Detection
Tool | Version | Checker | Description |
---|---|---|---|
1.2 | CC2.PRE03 | Fully implemented | |
9.7.1 | 79 S | Fully implemented | |
PRQA QA-C | Unable to render {include} The included page could not be found. | 3413 | Fully implemented |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
CERT C++ Secure Coding Standard | PRE03-CPP. Prefer typedefs to defines for encoding types |
ISO/IEC TR 24772:2013 | Pre-processor Directives [NMP] |
Bibliography