C provides a storage-compaction capability for structure members, in which each member occupies only a specified number of bits. Such a member is known as a bit-field. Bit-fields can be useful for reducing the storage needed for a large array of structures. They are also useful for defining various hardware interfaces which specify the individual bits within a machine word.
In portable code, do not depend upon the allocation order of bit-fields in memory. Of course, in machine-specific non-portable code one knows exactly how the bit-fields are laid out, and the internal details can be inspected with bitwise operations.
Consider the representation of time-of-day in hours, minutes, seconds and milliseconds. Bit-fields provide one way to represent such times:
typedef struct time_day { unsigned h1 : 2; {0:2} unsigned h2 unsigned m1 unsigned m2 unsigned s1 unsigned s2 unsigned f1 unsigned f2 unsigned f3 {0:9} {0:5) {0:9) {0:5) {0:9) {0:9} {0:9} {0:9} } TIME_DAY; /* 32 bits total */
The last millisecond of the day is
23:59:59.999 (hh:mm:ss.fff)
Each member (bit-field) is declared to be unsigned (int); this is the only bit-field type that is guaranteed to be portable to all current compilers. Each member is declared to have only as many bits as are necessary to represent the possible digits at its position in the time representation. Representing h1 (first digit of hours) takes only two bits to represent the possible values (0, 1, and 2). And the largest members need only four bits to represent ten digits, 0 through 9. The total number of bits is 32.
Consecutive bit-field members are allocated by the compiler to the same int-sized word, as long as they fit completely. Thus, on a 32-bit machine, a TIME_DAY object will occupy exactly one int-sized word. On a 16-bit machine, the first five members (totalling 16 bits) will fit into one int-sized word, and the last four members will fit into an immediately following word. Such an exact fit is rare, however. Add another member such as "day-of-year" to the structure, and the nice size-fitting property disappears. Thus, bit-fields are useful for storage-saving only if they occupy most or all of the space of an int, and if the storage-saving property is to be reasonably portable, they must occupy most of the space in a 32-bit integer 6-1.
The order of allocation within a word is different in different implementations. Some implementations are "right-to-left": the first member occupies the low-order position of the word. Most PDP-11 and VAX compilers allocate right-to-left. Following the convention that the ]ow-order bit of a word is on the right, the right-to-]eft allocation would look like this:
f3 f2 f1 s2 s1 m2 ml h2 h1
Most other implementations are "left-to-right":
h1 h2 ml m2 s1 s2 f1 f2 f3
A union provides a convenient way to say what is going on:
typedef union time_overlay /* MACHINE DEPENDENT */
{
struct time_day time_as_fields;
long time_as_long;
} TIME_OVERLAY;
TIME_OVERLAY time_port;
This allows bitwise operations like time_port.time_as_long & 0xF00 as well as providing access via bit-field names like time_port.time_as_fields.h1.
Specifying a field size of zero causes any subsequent allocations to begin on a new word boundary. Un-named bit-fields are allowed; they occupy space but are inaccessible, which is useful for "padding" within a structure.
Because most C machines do not support bit-addressing, the "address-of" (&) operator is not allowed upon bit-field members.
Aside from these complications, bit-fields can be treated just like any other structure member. The following declaration
#include "time_day.h" struct time_day last_msec = {2, 3, 5, 9, 5, 9, 9, 9, 9}; initializes last_msec to the last millisecond of the day. struct time_day now; /* ... */ if (now.h1 == 0 || (now.h1 == 1 && now.h2 < 2))
tests whether now is less than "noon."
If we wish to use the TIME_DAY structure for an "information-hiding" purpose, so that it could be changed without affecting the programs that use it, we should employ the "leading-underscore" convention mentioned earlier in Section 6.2 :
typedef struct_time_day { unsigned_h1 : 2; /* tens digit of hours {0:2} */ unsigned_h2 : 4; /* units digit of hours {0:9} */ unsigned_ml : 3; /* tens digit of minutes {0:5} */ unsigned_m2 : 4; /* units digit of minutes {0:9} */ unsigned_s1 : 3; /* tens digit of seconds {0:5) */ unsigned_s2 : 4; /* units digit of seconds {0:9} */ unsigned_fl : 4; /* first digit of fraction {0:9} */ unsigned_f2 : 4; /* second digit of fraction {0:9} */ unsigned_f3 : 4; /* third digit of fraction {0:9} */ } TIME_DAY; /* 32 bits total */
The TIME_DAY example illustrates the use of bit-fields nicely, but there are numerous other ways to represent time-of-day.
Risk Assessment
Making invalid assumptions about the type of a bit-field or its layout can result in unexpected program flow.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
INT11-A |
1 (low) |
1 (unlikely) |
2 (medium) |
P2 |
L3 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
[[ISO/IEC 9899-1999]] Section 6.7.2, "Type specifiers"
[[MISRA 04]] Rule 3.5, Rule 6.4, "Bit fields shall only be defined to be of type unsigned int or signed int"
[[Plum 85]] Rule 6-5