Integer types in C have both a size and a width. 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 of an integer type is the number of bits it uses to represent values, excluding any optional padding bits. Padding bits contribute to the integer's size, but not to its width. Consequently, inferring the width of an integer type from its size may result in a width 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 in your code, and in particular, do not use the sizeof operator to compute the width 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.
#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 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 the number of width bits used by any unsigned int type.
#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 width = 0;
while (num != 0) {
if (num % 2 == 1) {
width++;
}
num >>= 1;
}
return width;
}
#define UWIDTH(umax_value) popcount(umax_value)
The revised version of the pow2() function uses the UWIDTH() macro to determine the width of the unsigned type.
#include <stddef.h>
#include <stdint.h>
#include <limits.h>
#include <inttypes.h>
extern size_t popcount(uintmax_t);
#define UWIDTH(umax_value) popcount(umax_value)
unsigned int pow2(unsigned int exp) {
if (exp >= UWIDTH(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.
#define UWIDTH(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 including, for example, 32-bit and 64-bit x86 platforms.
#define UWIDTH(type) (sizeof(type) * CHAR_BIT)
Risk Assessment
Mistaking an integer size for its width can permit invalid width arguments to operations such as bitwise shifts, resulting in undefined behavior.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
INT19-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 |


