You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 110 Next »

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. 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

This noncompliant example will not compile, because macros use textual substitution and not the type system:  [also doesn't work with a single variable per line]

#define MATRIX double matrix[4][4]
MATRIX matrix_a, matrix_b;

Compliant Solution

This compliant solution compiles correctly.

typedef double matrix[4][4]; 
matrix matrix_a, matrix_b;

 

This code example also violates DCL04-C. Do not declare more than one variable per declaration. [is doing so necessary or useful for the example]

Noncompliant Code Example

I don't actually know what is wrong with this:

#define uchar unsigned char

 

Compliant Solution

Use type definitions to encode all non-pointer types.

typedef unsigned char uchar;

 

Risk Assessment

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

PRE03-C

Low

Unlikely

Medium

P2

L3

Automated Detection

ToolVersionCheckerDescription

ECLAIR

1.2

CC2.PRE03

Fully implemented

LDRA tool suite

9.7.1

79 S
273 S

Fully implemented

PRQA QA-C
Unable to render {include} The included page could not be found.
3413Fully implemented

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

Bibliography

 


 

  • No labels