Integer Error –”You Can’t Count That High” – CS0
Integer values that are too large or too small may fall outside the allowable bounds for their data type, leading to unpredictable problems that can both reduce the robustness of your code and lead to potential security problems.
Declaring a variable as type int allocates a fixed amount of space in memory. Most languages include several integer types, including short, int, long, etc. , to allow for less or more storage. The amount of space allocated limits the range of values that can be stored. For example, a 32-bit int variable can hold values from -231 through 231-1.
Input or mathematical operations such as addition, subtraction, and multiplication may lead to values that are outside of this range. This results in an integer error or overflow, which causes undefined behavior and the resulting value will likely not be what the programmer intended. Integer overflow is a common cause of software errors and vulnerabilities.
Risk – How Can It Happen?
An integer error can lead to unexpected behavior or may be exploited to cause a program crash, corrupt data, lead to incorrect behavior, or allow the execution of malicious software.
Example of Occurrence:
1. There is a Facebook group called “If this group reaches 4,294,967,296 it might cause an integer overflow.” This value is the largest number that can fit in a 32 bit unsigned integer. If the number of members of the group exceeded this number, it might cause an overflow. Whether it will cause an overflow or not depends upon how Facebook is implemented and which language is used – they might use data types that can hold larger numbers. In any case, the chances of an overflow seem remote, as roughly 2/3 of the people on earth would be required to reach the goal of more than 4 billion members.
2. On December 25, 2004, Comair airlines was forced to ground 1,100 flights after its flight crew scheduling software crashed. The software used a 16-bit integer (max 32,768) to store the number of crew changes. That number was exceeded due to bad weather that month which led to numerous crew reassignments.
Code Responsibly– How Can I Avoid Integer Error?
1. Know your limits: Familiarize yourself with the ranges available for each data type. Run Program 1 below to help you learn the sizes.
2. Choose your data types wisely: Many programming languages contain multiple data types for storing integer values. If you have any concerns about the integer values that you will be using, learn about the options available in the language you are using, and choose integer types that are large enough to hold the values you will be using.
3. Validate your input: Check input for range and reasonableness before conducting operations. (More on this later.)
integer i integer j integer result Display "For this compiler: integers are: " sizeof (int) " bytes " Display "largest integer is " INT_MAX Display "smallest integer is " INT_MIN Display "Input two integer values " Input i, j Display "You entered the following integers: " i " " j
- Run/execute (trace) the above pseudocode by inputting reasonable integer values.
- What is the largest possible integer value?
- Add the following lines to the pseudocode above
result = i * 10 Display "Your number times ten is " result result = i + j Display "The sum of your numbers is " result result = i * j; Display "The product of your numbers is " result
Trace for a large number to see if you create an error. Did you get an error when you type in 1 million (1,000,000)? 1 billion (1,000,000,000)? 10 billion (10,000,000,000)?
Complete the security checklist for this program (print the checklist).
What happens when a program exceeds the largest integer value? Explain.
How could addition result in an integer overflow?
How could multiplication result in an integer overflow?
- What operation is most likely to cause an integer overflow?
|Vulnerability: Integer Errors Course: CS0|
|Check each line of code||Completed|
|1. Underline each occurrence of an integer variable.|
|For each underlined variable:|
|2. Mark with a V any input operations that assign values to the variable.|
|3. Mark with a V any mathematical operations involving the variable.|
|4. Mark with a V any assignments made to the variable.|
|Highlighted areas indicate vulnerabilities!|
- What is the largest possible value of type int? Explain your answer using the information you read in the Background section.
- What happens when the result of an operation on values of type int exceeds this value? Explain.
Further Work (optional – check with your instructor if you need to answer the following questions)
- Look up the following info:
- What is the population of the US?
- What is the population of the world?
- What is the national debt?
- For which of the above would the integer data type be a problem?
- Discuss the Comair problem described above. What are the repercussions of such a problem?