Versions Compared

Key

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

...

Code Block
bgColor#FFCCCC
langc
#include <assert.h>
 
void func(void) {
  char c = 'x';
  int *ip = (int *)&c; /* This can lose information. */
  char *cp = (char *)ip;
  assert(cp == &c);    /* Will fail on some conforming implementations */

  /* ... */
}

...

Code Block
bgColor#FFCCCC
langc
#include <assert.h>
 
void func(void) {
  char c = 'x';
  int *ip = (int *)&c; /* This can lose information. */
  char *cp = (char *)ip;
  assert(cp == &c);    /* Will fail on some conforming implementations */

  /* ... */
}

...

Code Block
bgColor#ccccff
langc
#include <stdalign.h>  /* For alignas() */
#include <assert.h>
 
void func(void) {
  alignas(int) char c = 'x'; /* Align c to the alignment of an int */
  int *ip = (int *)&c; 
  char *cp = (char *)ip;
  assert(cp == &c);    /* Both cp and &c point to equally aligned objects. */
  /* ... */
}

Risk Assessment

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

EXP36-C

lowLow

probableProbable

mediumMedium

P4

L3

Automated Detection

Tool

Version

Checker

Description

Compass/ROSE  

Can detect violations of this rule. However, it does not flag explicit casts to void * and then back to another pointer type

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.EXP36

Fully implemented
EDG   
GCC
Include Page
GCC_V
GCC_V
 

Can detect some violations of this rule when the -Wcast-align flag is used

LDRA tool suite

Include Page
LDRA_V
LDRA_V

94 S
540 S

Fully implemented
PRQA QA-C
Include Page
PRQA_V
PRQA_V
3305Fully implemented

...