
Conversions can occur explicitly as the result of a cast or implicitly as required by an operation. While Although conversions are generally required for the correct execution of a program, they can also lead to lost or misinterpreted data. Conversion of an operand value to a compatible type causes no change to the value or the representation [ISO/IEC 9899:2011].
The C integer conversion rules define how C compilers handle conversions. These rules include integer promotions, integer conversion rank, and the usual arithmetic conversions. The intent of the rules is to ensure that the conversions result in the same numerical values and that these values minimize surprises in the rest of the computation. Prestandard C usually preferred to preserve signedness of the type.
...
Every integer type has an integer conversion rank that determines how conversions are performed. The ranking is based on the concept that each integer type contains at least as many bits as the types ranked below it. The following rules for determining integer conversion rank are defined in the C standardStandard, Section 6.3.1.1 [ISO/IEC 9899:2011]:
- No two signed integer types shall have the same rank, even if they have the same representation.
- The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision.
- The rank of
long long int
shall be greater than the rank oflong int
, which shall be greater than the rank ofint
, which shall be greater than the rank ofshort int
, which shall be greater than the rank ofsigned char
. - The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.
- The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width.
- The rank of
char
shall equal the rank ofsigned char
andunsigned char
. - The rank of
_Bool
shall be less than the rank of all other standard integer types. - The rank of any enumerated type shall equal the rank of the compatible integer type.
- The rank of any extended signed integer type relative to another extended signed integer type with the same precision is implementation-defined , but still subject to the other rules for determining the integer conversion rank.
- For all integer types
T1
,T2
, andT3
, ifT1
has greater rank thanT2
andT2
has greater rank thanT3
, thenT1
has greater rank thanT3
.
...
The programmer must be careful when performing operations on mixed types. This noncompliant code example shows an idiosyncracy idiosyncrasy of integer promotions.
Code Block | ||||
---|---|---|---|---|
| ||||
int si = -1; unsigned int ui = 1; printf("%d\n", si < ui); |
In this example, the comparison operator operates on a signed int
and an unsigned int
. By the conversion rules, si
is converted to an unsigned int
. Because −1 cannot be represented as an unsigned int
value, the −1 is converted to UINT_MAX
. This is in accordance with C, Section 6.3.1.3, para. 2 [ISO/IEC 9899:2011]:
Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
Consequently, the program prints 0 , because UINT_MAX
is not less than 1.
...
This vulnerability in Adobe Flash arises because Flash passes a signed integer to calloc()
. An attacker has control over this integer and can send negative numbers. Because calloc()
takes size_t
, which is unsigned, the negative number is converted to a very large number, which is generally too big to allocate, and as a result, calloc()
returns NULL
, causing the vulnerability to exist.
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
...
...
...
Numeric conversion errors |
...
...
...
Integer coercion error |
...
...
...
Numeric truncation error |
...
Bibliography
[Dowd 2006] | Chapter 6, "C Language Issues" ("Type |
---|
...
Conversions," pp. 223–270) | |
---|---|
[ISO/IEC 9899:2011] | Section 6.3, "Conversions" |
[Seacord 2005a] | Chapter 5, "Integers" |