Versions Compared

Key

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

...

If the misaligned pointer is dereferenced, the program may terminate abnormally. The cast alone may cause a loss of information, even if the value is not dereferenced. For example, the following code is not guaranteed to work conforming C99 implementations, even though no pointers are dereferenced:

Code Block

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

char *loop_ptr;
int *int_ptr;

int *loop_function(void *v_pointer) {
  /* ... */
  return v_pointer;
}
int_ptr = loop_function(loop_ptr);

...

Code Block
bgColor#ccccff
langc

int *loop_ptr;
int *int_ptr;

int *loop_function(int *v_pointer) {
  /* ... */
  return v_pointer;
}
int_ptr = loop_function(loop_ptr);

...

Code Block
bgColor#FFCCCC
langc

char *data;
struct foo_header *tmp;
struct foo_header *header;

tmp = data + offset;
memcpy(&header, tmp, sizeof(header));

if (header.len < FOO)
/* ... */

...

Code Block
bgColor#ccccff
langc

char *data;
struct foo_header header;

memcpy(&header, data + offset, sizeof(header));

if (header.len < FOO)
/* ... */

...

Tool

Version

Checker

Description

Section

LDRA tool suite

Include Page
LDRA_V
LDRA_V
Section

94 S
540 S

Section

Fully Implemented

Section

GCC

Include Page
GCC_V
GCC_V

 

Section

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

Section

EDG Front End to Compass/ROSE

 

 

 

Section

Compass/ROSE

 

 

Section

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

Section

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V
Section

castexpr

Section

Fully Implemented

...