Integer types in C have both a size and a width precision. The size indicates the number of bytes used by an object, and can be retrieved for any object or type using the sizeof operator. The width precision of an integer type is the number of bits it uses to represent values, excluding any optional sign and padding bits.
Padding bits contribute to the integer's size, but not to its widthprecision. Consequently, inferring the width precision of an integer type from its size may result in a width precision that is larger than it actually is which can then lead to incorrect assumptions about the range of these types. Make sure you use correct integer widths precisions in your code, and in particular, do not use the sizeof operator to compute the width precision of an integer type on architectures that use padding bits or in strictly conforming (that is, portable) programs.
Noncompliant Code Example
This noncompliant code example illustrates a function that produces 2 raised to the power of the function argument. To prevent undefined behavior, in compliance with INT34-C. Do not shift a negative number of bits or more bits than exist in the operand, the function ensures that the argument is less than the number of bits used to store an unsigned int.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <limits.h>
unsigned int pow2(unsigned int exp) {
if (exp >= sizeof(unsigned int) * CHAR_BIT) {
/* Handle error */
}
return 1 << exp;
} |
However, if this code runs on a platform where unsigned int has one or more padding bits, it can still accept values for exp that are too large. For example, a platform that stores unsigned int in 64 bits, but uses only 48 width bits to represent the value, could perform a left shift on an illegal value of 56.
Compliant Solution
This compliant solution uses a popcount() function, which counts the number of bits set on any unsigned integer. This allows this code to count determine the number precision of width bits used by any unsigned int typeany integer type, signed or unsigned.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stddef.h>
#include <stdint.h>
#include <limits.h>
#include <inttypes.h>
/* Returns the number of set bits */
size_t popcount(uintmax_t num) {
size_t widthprecision = 0;
while (num != 0) {
if (num % 2 == 1) {
widthprecision++;
}
num >>= 1;
}
return widthprecision;
}
#define UWIDTHPRECISION(umax_value) popcount(umax_value)
|
The revised version of the pow2() function uses the UWIDTHPRECISION() macro to determine the width precision of the unsigned type.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stddef.h> #include <stdint.h> #include <limits.h> #include <inttypes.h> extern size_t popcount(uintmax_t); #define UWIDTHPRECISION(umax_value) popcount(umax_value) unsigned int pow2(unsigned int exp) { if (exp >= UWIDTHPRECISION(UINT_MAX)) { /* handle error */ } return 1 << exp; } |
Implementation Details
Some platforms, such as the Cray Linux Environment (CLE) provide a _popcnt instruction which can substitute for our popcount() function.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#define UWIDTHPRECISION(umax_value) _popcnt(umax_value) |
Exceptions
INT19-EX1: Platforms in which integer types have no padding may use the sizeof operator to compute integer widths precision including, for example, 32-bit and 64-bit x86 platforms.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#define UWIDTHPRECISION(type) (sizeof(type) * CHAR_BIT) |
Risk Assessment
Mistaking an integer size for its width precision can permit invalid width precision arguments to operations such as bitwise shifts, resulting in undefined behavior.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
INT19INT35-C | low | unlikely | medium | P2 | L3 |
Bibliography
| [Dowd 2006] | Chapter 6, "C Language Issues" |
| [C99 Rationale 2003] | Subclause 6.5.7, "Bitwise Shift Operators" |
Cray Linux Environment (CLE): supported on Cray XT CNL compute nodes |
...


