Lecture 12 Integers Computer and Network Security 19th of December 2016 Computer Science and Engineering Department CSE Dep, ACS, UPB Lecture 12, Integers 1/40
Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 2/40
Integers numbers non-floating point positive, negative or zero CSE Dep, ACS, UPB Lecture 12, Integers 3/40
Integer Use actual numbers and integers values and... array index length/size of an object pointer arithmetic loop counter, array bound argument to a memory allocation function CSE Dep, ACS, UPB Lecture 12, Integers 4/40
Data Types char short int long long long pointer CSE Dep, ACS, UPB Lecture 12, Integers 5/40
Size of Data Types. Data Models CERT C Programming Language Secure Coding Standard, pg. 118 CSE Dep, ACS, UPB Lecture 12, Integers 6/40
To Bear in Mind representation of data types bounds (minimum, maximum) signedness conversion pointers and integers CSE Dep, ACS, UPB Lecture 12, Integers 7/40
Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 8/40
Positive Integers always the same first bit is 0 for signed integers and the rest is the value the actual value is stored for unsigned integers representation differs for (signed) negative integers CSE Dep, ACS, UPB Lecture 12, Integers 9/40
Signed-magnitude for Negative Integers direct representation first bit is 1 the rest of the bits are the actual value issue: difficult hardware implementation CSE Dep, ACS, UPB Lecture 12, Integers 10/40
1 s Complement for Negative Integers first bit is 1 the rest of the bits are complemented (reversed) the circuitry is simpler issue: two values for zero (positive and negative zero) CSE Dep, ACS, UPB Lecture 12, Integers 11/40
2 s Complement for Negative Integers first bit is 1 the rest of the bits are complemented and added one a single (positive) representation for zero CSE Dep, ACS, UPB Lecture 12, Integers 12/40
Signed/Unsigned Representation and Ranges we use 2 s complement consider N bits width representation 2 N possible values for unsigned integers range is (0... 2 N 1) for signed integers, range is ( 2 N 1... 2 N 1 1) CSE Dep, ACS, UPB Lecture 12, Integers 13/40
Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 14/40
Promotions integers smaller than int are promoted to int when performing operations done to avoid overflows char c1, c2; c1 = c1 + c2; CSE Dep, ACS, UPB Lecture 12, Integers 15/40
Types and Ranks type: signed or unsigned ranks: long long > long > int > short > char convert to the larger rank CSE Dep, ACS, UPB Lecture 12, Integers 16/40
Unsigned to Unsigned safe from shorter range to larger range truncation if converting from larger range to shorter range CSE Dep, ACS, UPB Lecture 12, Integers 17/40
Unsigned to Signed same rank high-order bit is signed bit sign may change CSE Dep, ACS, UPB Lecture 12, Integers 18/40
Signed to Signed sign-extension is convert to larger rank preserve lower part in case of conversion to a smaller rank CSE Dep, ACS, UPB Lecture 12, Integers 19/40
Signed to Unsigned value unchanged if signed integer is positive truncation if unsigned rank is smaller sign-extension if larger unsigned rank in case of negative integers, a large positive integers is obtained CSE Dep, ACS, UPB Lecture 12, Integers 20/40
Signed/Unsigned Characters char may be signed char on unsigned char when storing negative signed char in an integer, the result is a large negative number if value above 127 (0x7F) use unsigned char CSE Dep, ACS, UPB Lecture 12, Integers 21/40
In a Nutshell CSE Dep, ACS, UPB Lecture 12, Integers 22/40
Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 23/40
Addition integer and integer pointer and integer may result in overflow arithmetic conversion CSE Dep, ACS, UPB Lecture 12, Integers 24/40
Subtraction integer and integer pointer and integer may result in negative overflow CSE Dep, ACS, UPB Lecture 12, Integers 25/40
Multiplication prone to overflow even for small operands good idea to allocate twice the storage for the product CSE Dep, ACS, UPB Lecture 12, Integers 26/40
Division division by zero division of minimum signed integer value divided by -1 CSE Dep, ACS, UPB Lecture 12, Integers 27/40
Modulo similar to division, since division is involved careful at negative remainders CSE Dep, ACS, UPB Lecture 12, Integers 28/40
Shifting careful when shifting signed negative integers integer promotion must be taken into consideration CSE Dep, ACS, UPB Lecture 12, Integers 29/40
Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 30/40
Truncation convert to a smaller integer type the initial value is outside range high order bits are lost in case of converting from larger to shorter CSE Dep, ACS, UPB Lecture 12, Integers 31/40
Overflow may be signed on usigned go beyond maximum value or below minimum value CSE Dep, ACS, UPB Lecture 12, Integers 32/40
Sign Error convert unsigned to signed if bit sign in integer is set it results in a negative number convert signed to unsigned negative numbers result in large positive numbers CSE Dep, ACS, UPB Lecture 12, Integers 33/40
Negative Indices negative arrays index value outside bounds CSE Dep, ACS, UPB Lecture 12, Integers 34/40
Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 35/40
Recommendations INT00-C to INT18-C on 04. Integers (INT) in CERT C Secure Coding Standard CSE Dep, ACS, UPB Lecture 12, Integers 36/40
Rules INT30-C to INT36-C on 04. Integers (INT) in CERT C Secure Coding Standard CSE Dep, ACS, UPB Lecture 12, Integers 37/40
Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 38/40
Keywords integer type long long, long, int, short, char signed, unsigned bounds/range 2 s complement conversion promotion rank addition subtraction multiplication division modulo truncation overflow sign error negative index CSE Dep, ACS, UPB Lecture 12, Integers 39/40
References CERT C Secure Coding Standard 04. Integers (INT) https://www.securecoding.cert.org/confluence/ pages/viewpage.action?pageid=270 Secure Coding in C and C++ Class Module 3. Integers Secure Coding in C and C++ Chapter 5. Integer Security CSE Dep, ACS, UPB Lecture 12, Integers 40/40