Versions Compared

Key

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

Weak typing in C allows type casting memory to different types. Because the internal representation of most types is system dependent, applying operations intended for data of one type to data of a different type will likely yield non-portable nonportable code and produce unexpected results.

...

Bit fields can be used to allow flags or other integer values with small ranges to be packed together to save storage space. Bit fields can improve the storage efficiency of structures. Compilers typically allocate consecutive bit-field structure members into the same int-sized storage, as long as they fit completely into that storage unit. However, the order of allocation within a storage unit is implementation-defined. Some implementations are "right-to-left": the first member occupies the low-order position of the storage unit. Others are "left-to-right": the first member occupies the high-order position of the storage unit. Calculations that depend on the order of bits within a storage unit may produce different results on different implementations.

...

Making invalid assumptions about the type of type-cast data, especially bit - fields, can result in unexpected data values.

...

sectioncan

Can detect violations of this recommendation. Specifically, it reports violations if

A pointer
    • a pointer to one object is type cast to the pointer of a different object.
The
    • the pointed-to object of the (type cast) pointer is then modified arithmetically.

LDRA tool suite

94 S
95 S

section

Tool

Version

Checker

Description

Compass/ROSE

 

 

Section
Section
Include Page
LDRA_V
LDRA_V
Section

Fully

Implemented

implemented.

Related Vulnerabilities

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

...

CERT C++ Secure Coding Standard: EXP11-CPP. Do not apply operators expecting one type to data of an incompatible type

ISO/IEC 9899:1999 Section 2011 Section 6.7.2, "Type specifiers"

ISO/IEC TR 17961 (Draft) Accessing an object through a pointer to an incompatible type [ptrcomp]

ISO/IEC TR 24772 "STR Bit Representationsrepresentations"

MISRA Rule 3.5

Bibliography

...