Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Removed the row for Polyspace Bug Finder support.

...

  • The alignment of bit-fields in the storage unit . For (for example, the bit-fields may be allocated from the high end or the low end of the storage unit.)
  • Whether or not bit-fields can overlap a storage unit boundary.

Consequently, it is impossible to write portable safe code that makes assumptions regarding the layout of bit-field structure members.

...

Code Block
bgColor#ffcccc
langc
struct bf {
  unsigned int m1 : 8;
  unsigned int m2 : 8;
  unsigned int m3 : 8;
  unsigned int m4 : 8;
}; /* 32 bits total */

void function() {
  struct bf data;
  unsigned char *ptr;

  data.m1 = 0;
  data.m2 = 0;
  data.m3 = 0;
  data.m4 = 0;
  ptr = (unsigned char *)&data;
  (*ptr)++; /* canCan increment data.m1 or data.m4 */
}

...

This compliant solution is explicit in which fields it modifies.:

Code Block
bgColor#ccccff
langc
struct bf {
  unsigned int m1 : 8;
  unsigned int m2 : 8;
  unsigned int m3 : 8;
  unsigned int m4 : 8;
}; /* 32 bits total */

void function() {
  struct bf data;
  data.m1 = 0;
  data.m2 = 0;
  data.m3 = 0;
  data.m4 = 0;
  data.m1++;
}

Noncompliant Code Example (Bit-Field Overlap)

In the following this noncompliant code example, assuming 8 bits to a byte, if bit-fields of 6 and 4 bits are declared, is each bit-field contained within a byte, or are the bit-fields split across multiple bytes?

Code Block
bgColor#ffcccc
langc
struct bf {
  unsigned int m1 : 6;
  unsigned int m2 : 4;
};

void function() {
  unsigned char *ptr;
  struct bf data;
  data.m1 = 0;
  data.m2 = 0;
  ptr = (unsigned char *)&data;
  ptr++;
  *ptr += 1; /* whatWhat does this increment? */
}

...

This compliant solution is explicit in which fields it modifies.:

Code Block
bgColor#ccccff
langc
struct bf {
  unsigned int m1 : 6;
  unsigned int m2 : 4;
};

void function() {
  struct bf data;
  data.m1 = 0;
  data.m2 = 0;
  data.m2 += 1;
}

...

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

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

EXP11-C

medium

Medium

probable

Probable

medium

Medium

P8

L2

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

Supported: Astrée reports runtime errors resulting from invalid assumptions.
Compass/ROSE

 

 



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
Helix QAC

Include Page

LDRA

Helix QAC_V

LDRA

Helix QAC_V

94 S
95 S

Fully implemented.

PRQA QA-C
C0310, C0751
LDRA tool suite
Include Page
LDRA
Include PagePRQA
_V
PRQA
LDRA_V
0310

554 S

Partially

Fully implemented

.

Related Vulnerabilities

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

Related Guidelines

SEI CERT C++
Secure
Coding StandardVOID EXP11-CPP. Do not apply operators expecting one type to data of an incompatible type
ISO/IEC TR 24772:2013Bit
representations
Representations [STR]
MISRA
-
C:2012
Rule 3
Directive 1.
5
1 (required)
: If it is being relied upon, the implementation-defined behaviour and packing of bitfields shall be documented

Bibliography

[Plum 1985]Rule 6-5: In portable code, do not depend upon the allocation order of bit-fields within a word

...


...

Image Modified Image Modified Image Modified