Ask the Expert

Understanding and preventing integer overflows

What are integer overflows? How do they differ from buffer overflows? What kind of damage do they cause and how can I prevent them?

    Requires Free Membership to View

An integer overflow occurs whenever a computer program tries to store an integer into a register or variable that isn't large enough to hold that value. Generally, the size of these locations is determined by the size of the register on the processor being used, usually 32 bits. Buffer overflows are a different sort of exploit. (For an introduction to buffer overflows, see the chapter on buffer overflows from the OWASP Guide to Building Secure Web Applications and Web Services.)

Math operations such as addition, multiplication and shifts can produce a result that is too large to store -- an integer overflow. Depending on the compiler, this integer overflow can result in a sign error, truncating the largest or smallest portion of the result, or another type of error. An integer overflow could lead to a security problem if the overflow affects the value of a pointer that references other code or data in memory. An attack could exploit the integer overflow in a way that allows execution of arbitrary code, resulting in a complete takeover of the vulnerable program's process.

These flaws can be extremely tricky to find and eliminate. The best approach is to use a safe integer class that has been built to avoid these problems. David LeBlanc's column "Integer Handling with the C++ SafeInt Class" provides a detailed mathematical analysis of integer overflows.

This was first published in June 2006

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: