Great work!

XP to next level

BugEater

Integer Overflow — What Happens Next?

Learning Objectives

By the end of this lesson you will be able to:

  • Describe the two main overflow behaviors: wrap-around and exception
  • Explain why wrap-around is more dangerous than an exception
  • Identify overflow by symptom (unexpected negative results, wrong calculations)

What Overflow Looks Like

Int32 can hold values from -2,147,483,648 to 2,147,483,647. What happens when you add 1 to the maximum?

In C and many systems languages: The value wraps around to -2,147,483,648. No error. No exception. Completely silent. Suddenly your "very large positive number" has become a very large negative number.

In Java with int: Same as C — wrap-around. Java int overflow is silent.

In Java with Math.addExact(): An ArithmeticException is thrown. The developer chose to detect overflow.

In Python: Python integers have no fixed size limit. They grow automatically. No overflow possible.

In databases (PostgreSQL): INSERT of an overflowed integer throws an error: "integer out of range."

The Danger of Silent Wrap-Around

Imagine a retail system that calculates a discount: discountAmount = totalPrice * discountPercent / 100. If totalPrice is stored as Int32 and the order is very large, totalPrice * discountPercent might overflow before the division.

The result: a wildly wrong discount amount. No error is thrown. The transaction completes. The customer gets either a massive over-discount or pays too much.

This is why overflow is a data integrity issue, not just a technical curiosity.

Recognizing Overflow by Symptom

Symptom Likely Cause
Large positive number → suddenly becomes large negative number Int overflow wrap-around
Calculation gives wrong result for large inputs but correct for small ones Intermediate calculation overflow
"ArithmeticException: integer overflow" in error log Java/caught overflow
"integer out of range" from database PostgreSQL INT overflow
Application crashes at a specific large input value Uncaught overflow exception

The Off-By-One At the Limit

When testing Int32 max (2,147,483,647):

  • Input exactly 2,147,483,647 → what happens?
  • Input 2,147,483,648 → what happens?

In Java: 2,147,483,647 + 1 = -2,147,483,648. The overflow point is between these two values. If the application doesn't validate, the second input either causes a parsing error (if the backend parses as int), wraps around (if it's an in-memory calculation), or fails at the database (NOT NULL and range constraints).

Pro Tip: Look for "unexpected negative results" as a symptom in production bugs. If a financial system shows a customer owes -$3,000,000, overflow is a prime suspect.

Key Takeaways

  • Overflow in C/Java int is silent wrap-around — no exception, just a wrong value
  • Math.addExact() in Java and similar patterns can catch overflow explicitly
  • Python integers don't overflow; databases have hard integer range constraints
  • Test both the exact limit (passes) and limit+1 (should either error or overflow)

Quiz

In Java, what is the result of Integer.MAX_VALUE + 1?

A shopping cart stores the total price as an Int32 cent value. A user adds items totalling $21,474,836.48 (Int32 max in cents). They add one more $0.01 item. What happens?

Which programming language construct PREVENTS integer overflow silently wrapping in Java?

How should a tester report an integer overflow bug to make it actionable for the developer?