C Language Issues Part 2
C Language Issues Part 2
Institute for
Recap
Cybersecurity
How the compiler chooses which type conversions to apply in the context of C
expressions,
• You can look at some situations where these type conversions occur
Casts
• As you know, typecasts are C's mechanism for letting programmers
specify an explicit type conversion
Canadian
Institute for
Cybersecurity
Simple Conversions
Assignments
• The compiler must convert the type of the right operand into the type of
the left operand
Canadian
Institute for
Cybersecurity
Simple Conversions
Function calls
Canadian
Institute for
Cybersecurity
Simple Conversions
• Integer promotions specify how C takes a narrow integer data type, such as
a char or short, and converts it to an int (or, in rare cases, to an unsigned int).
https://wiki.sei.cmu.edu/confluence/display/c/INT02-C.+Understand+integer+conversion+rules
Canadian
Institute for
Cybersecurity
Integer Promotions
• Some data types like char , short int take less number of bytes than int.
• These data types are automatically promoted to int or unsigned int when an
operation is performed on them. This is called integer promotion
- no arithmetic calculation happens on smaller types like char, short and enum.
- They are first converted to int or unsigned int and then arithmetic is done on
them.
- If an int can represent all values of the original type, the value is converted to
an int . Otherwise, it is converted to an unsigned int.
Canadian
Institute for
Cybersecurity
Integer Promotions
• The signed and unsigned varieties of each type are assigned the same
rank.
Canadian
Institute for
Cybersecurity
Integer Promotions
• Basically, any place in C where you can use an int or unsigned int, you can also use any
integer type with a lower integer conversion rank.
• The ranking is based on the concept that each integer type contains at least as many
bits as the types ranked below it.
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
Canadian
Institute for
Cybersecurity
Integer Promotions
• Second, if the variable is an integer type, but its integer conversion rank is greater than or
equal to that of an int, the promotions do nothing. Therefore, ints, unsigned ints, long ints,
pointers, and floats do not get altered by the integer promotions.
Canadian
Institute for
Cybersecurity
Canadian
Institute for
Cybersecurity
Canadian
Institute for
Cybersecurity
Usual Arithmetic Conversions
If you have two operands and neither is a float, you get into the rules for
reconciling integers
Canadian
Usual Arithmetic Conversions Institute for
Cybersecurity
The first rule for this situation is that if the unsigned operand is of greater integer conversion
rank than the signed operand, or their ranks are equal, you convert the signed operand to the
type of the unsigned operand.
int jim = -5; jim is a signed integer, and sizeof (int) is a size_t, which is an
unsigned integer type.
if (jim < sizeof (int)) Because size_t has a greater integer conversion rank, the unsigned
do_something(); type takes precedence by this rule. Therefore, jim is converted to an
unsigned integer type, the comparison fails, and do_something() is
not called
if (4294967291 < 4)
do_something();
Canadian
Institute for
Cybersecurity
Canadian
Institute for
Cybersecurity
Canadian
Institute for
Cybersecurity
Type Conversion Vulnerabilities
• Signed/Unsigned Conversions
• Sign Extension
• Truncation
• Conversion
Canadian
Institute for
Cybersecurity
Canadian
Institute for
Cybersecurity
Canadian
Institute for
Cybersecurity
Canadian
Institute for
Cybersecurity
Canadian
Institute for
Cybersecurity
Signed/Unsigned Conversions
• Most libc routines that take a size parameter have an argument of type
size_t, which is an unsigned integer type.
• This is why you must be careful never to let a negative length field make its
way to a libc routine, such as snprintf(), strncpy(), memcpy(), read(), or strncat().
Canadian
Institute for
Cybersecurity