Definition
Integer promotion and conversion are the C rules that change the type of an integer value. The integer promotions convert an operand whose type ranks below int in C’s ordering of integer types, such as char, short or uint8_t, to int before arithmetic, or to unsigned int if int cannot hold all its values. The usual arithmetic conversions then give the two operands of an operator one common type, in which the operation runs. A conversion also happens at assignment, at a function call, at return and at a cast.
The idea
A message handler reads a signed length into int len and rejects long messages with if (len > 64) reject();. An attacker sends . The test compares two int values, is false, and the message is accepted. The handler then calls memcpy(buf, src, len). The length parameter of memcpy has the unsigned type size_t, so the call converts to size_t, which gives the largest value of that type, on a 64-bit machine. The copy runs far past the 64-byte buffer.
Each step followed the C rules. The bug is that the program checked the value in one type and used it in another. C never applies an operator to two values of different types: it first converts them to one type, and it converts a value again whenever the value moves into a variable or parameter of another type. The declared types are only the starting point. To know what an expression computes, follow three steps in order: promote each operand, choose the common type and compute in it, then convert the result to the type of wherever it goes.
How it works
Step 1: integer promotion. An operand whose type ranks below int is converted to int if int can represent every value of the original type, and to unsigned int otherwise (C11, the 2011 edition of the ISO standard that defines the C language, section 6.3.1.1). This happens to the operands of arithmetic, bitwise and comparison operators and to each operand of a shift. The ranking, called the integer conversion rank, orders the type names from low to high: _Bool; char, signed char and unsigned char, which rank equal; short; int; long; long long. Each unsigned type has the rank of its signed partner. Rank follows the names, not the widths: long ranks above int even where both have 32 bits. Promotion never changes a value, but it changes the arithmetic. On a machine with a 32-bit int, with uint8_t a = 200, b = 100; the sum a + b is the int value 300, not 44; and with uint8_t x = 0x0F; the complement ~x is the int value , whose bits are 0xFFFFFFF0, so ~x == 0xF0 is false.
Step 2: the usual arithmetic conversions (C11, section 6.3.1.8). The arithmetic operators, the comparisons and the bitwise operators &, | and ^ choose one common type for their two promoted operands. The first rule that applies decides:
Both operands have the same type: use that type.
Both are signed, or both are unsigned: use the type of higher rank.
The unsigned operand’s type has rank greater than or equal to the signed operand’s type: convert the signed operand to the unsigned type.
The signed operand’s type can represent every value of the unsigned operand’s type: convert the unsigned operand to the signed type.
Otherwise, convert both operands to the unsigned type with the same rank as the signed operand’s type.
The operation then runs in the common type, and its result has that type. Shifts are the exception: they promote each operand separately, and the result has the type of the promoted left operand (see the Shift semantics §4.20 entry).
Step 3: conversion to the destination. When a value is assigned, passed to a parameter of a declared type, returned from a function or cast, it is converted to the new type by these rules (C11, section 6.3.1.3):
if the value fits in the new type, it is unchanged;
otherwise, if the new type is unsigned, is added or subtracted until the value fits, where is the number of value bits of the new type;
otherwise the new type is signed and the value does not fit, and the result is implementation-defined (or an implementation-defined signal is raised), meaning that each compiler chooses and documents what happens. GCC §12.17 (the GNU Compiler Collection, from the GNU project, whose name stands for GNU’s Not Unix) keeps the low bits.
The destination plays no part in steps 1 and 2. In uint64_t r = a * b; the multiplication runs in the common type of a and b, and only the finished product is converted. To compute in 64 bits, convert an operand before the operation: (uint64_t)a * b.
Worked example
Take the comparison i < n with int i = -1; and size_t n = 2; on 64-bit Linux, where int has 32 bits and size_t is unsigned long with 64 bits.
Promotion changes nothing, because neither type ranks below
int.The types differ in signedness.
unsigned longranks aboveint, so rule 3 applies andiis converted tounsigned long.Converting adds : .
The comparison is false.
On a 32-bit Arm microcontroller §6.1 size_t is unsigned int, which has the same rank as int, so rule 3 applies again, becomes , and the result is again false.
Rules 4 and 5 make the result depend on the machine. Compare -1L < 2u, a long with an unsigned int. On 64-bit Linux, long has 64 bits and can represent every 32-bit unsigned value, so rule 4 converts 2u to long, and is true. On 64-bit Windows and on 32-bit Arm, long has 32 bits and cannot represent , so rule 5 converts both operands to unsigned long: becomes , and the comparison is false. The same line of C gives opposite answers.
A hidden signed overflow §4.15: on a machine with a 32-bit int, let uint16_t p = 50000, q = 50000; and uint32_t r = p * q;. Both operands are promoted to int, because int holds every 16-bit value. The true product exceeds the largest int, , so the multiplication is a signed integer overflow, which is undefined behavior §4.12, although every declared type is unsigned. Writing (uint32_t)p * q makes the left operand unsigned int; rule 3 converts q to unsigned int, and the product fits below .
Stated precisely
Conversion of an integer value to an unsigned type with value bits gives the unique with and , for some integer ; that is, , the nonnegative remainder. For and : and . For and : and .
The promotion rule and the five rules of step 2 are exact as stated, but their outcome depends on the widths that each implementation chooses, which the header <limits.h> reports as INT_MAX, LONG_MAX and so on. Two further conversions complete the picture: a floating-point value converted to an integer type loses its fractional part, which rounds it toward zero, and the behavior is undefined if the remaining whole number does not fit (C11, section 6.3.1.4); and any nonzero value converted to _Bool becomes 1.
Limits and common mistakes
A cast after an operation is too late. (uint64_t)(a * b) converts a product that has already overflowed or wrapped.
Checking a value in one type and using it in another causes many of these bugs, as with the message handler that tested as an int and passed it to memcpy as a size_t. Check in the type in which the value will be used, or reject negative values first.
Conversion to a signed type does not wrap by definition. Storing 200 in an int8_t has an implementation-defined result; GCC gives , but C11 does not require that result.
Whether plain char is signed is implementation-defined: it is signed on x86 and unsigned on Arm. A char holding the byte 0xFF therefore promotes to on one and to 255 on the other (see the Truncation and extension §4.19 entry).
Integer constants have types too: 1 is an int and 1u an unsigned int, so a single u suffix can turn a whole comparison into an unsigned one.
GCC and Clang warn about comparisons between signed and unsigned operands under the option -Wsign-compare, which C programs get through -Wextra.
Questions answered
What type does a comparison use between a signed int and size_t?
On mainstream platforms the comparison uses the unsigned type behind size_t: the int operand is converted to that unsigned type before the comparison. The usual arithmetic conversions, the C rules that give both operands one common type, convert the signed operand to the unsigned type whenever the unsigned type’s integer conversion rank, C’s ordering of integer types, is at least that of the signed type. size_t is unsigned int on 32-bit platforms such as 32-bit Arm, unsigned long on 64-bit Linux and unsigned long long on 64-bit Windows, and each of these ranks at least as high as int.
A negative int therefore becomes a huge value: becomes with a 64-bit size_t, so -1 < (size_t)2 is false. The C standard requires only that size_t be some unsigned integer type; if an implementation chose one ranked below int, promotion would come first and the comparison could be signed, but mainstream implementations do not. The safe pattern tests the sign first: i >= 0 && (size_t)i < n, where && evaluates its right side only if i >= 0 holds.
Related entries: Signed integer overflow (§4.15), Unsigned modular arithmetic (§4.16), Signed versus unsigned comparison (§2.12), Truncation and extension (§4.19), Implementation-defined behavior (§4.13). Further reading: [78].