Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code uses the assert() macro to assert a property concerning a memory-mapped structure that is essential for the code to behave correctly.:

Code Block
bgColor#FFCCCC
langc
struct timer {
  unsigned char MODE;
  unsigned int DATA;
  unsigned int COUNT;
};
 
int func(void) {
  assert(sizeof(struct timer) == sizeof(unsigned char) + sizeof(unsigned int) + sizeof(unsigned int));
}

...

This portable compliant solution uses static_assert.:

Code Block
bgColor#ccccff
langc
struct timer {
  unsigned char MODE;
  unsigned int DATA;
  unsigned int COUNT;
};

static_assert(sizeof(struct timer) == sizeof(unsigned char) + sizeof(unsigned int) + sizeof(unsigned int),
              "Structure must not have any padding");

...

Tool

Version

Checker

Description

Compass/ROSE

 

 

Could detect violations of this rule merely by looking for calls to assert(), and if it can evaluate the assertion (due to all values being known at compile time), then the code should use static-assert instead. This ; this assumes ROSE can recognize macro invocation.

ECLAIR
Include Page
ECLAIR_V
ECLAIR_V
macrcallFully implemented.

LDRA tool suite

Include Page
LDRA_V
LDRA_V

44 S

Fully implemented.

PRQA QA-C
Include Page
PRQA_V
PRQA_V
2741 2742Partially implemented.

Related Vulnerabilities

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

...