Thread: Signed integer overflow - undefined behaviour

  1. #1
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    189

    Signed integer overflow - undefined behaviour

    Hello,

    as in subject the overflow of signed integers is defined as *undefined* - meaning that anything can happen including program crash. I think the *undefined* part comes from the fact that sigined numbers can be represented differently - sign and magnitude, one's complements two complements just to name a few. However I have no idea why would such overflow cause program crash?

    Do you have any idea, best with example of such *crash*?

  2. #2
    Registered User
    Join Date
    May 2016
    Posts
    104
    Not only crash, but it can be a security vulnerability that malicious entities can exploit. Personally, I've never had anything happen worse than my program printing nonsense to the console, but I did read a story of undefined behavior consistently producing a prompt asking the user if it really wanted to format the root partition. Good thing it asked...

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,084
    While it is unlikely that your program will "crash" simply because of signed integer overflow, it is possible that the overflow will create a meaningless value that causes your program to crash by how you use it later on. And 2's complement is essentially universal at this point in time.

    "Undefined" simply means that the standard says absolutely nothing about what is supposed to happen and that the implementation has no requirement to document what it does or to do anything sensible or to do the same thing every time or to continue to do the same thing on the next release.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    765
    One example of undefined behavior of signed integer overflow is when a compiler assumes that a signed integer "cannot" overflow. This can be used to optimize away expressions like the following:

    Code:
    int a = ...;
    // ...
    if (a + 1 < a) {}
    The compiler is free to assume that a signed integer cannot overflow (because it's undefined), so the entire "if" statement can be removed. Some programs use statements like that to check if an addition would overflow, but it needs to be replaced with a test that has defined behavior, such as this:

    Code:
    int a = ...;
    // ...
    if (a > INT_MAX - 1) {}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined behaviour
    By Saurabh Mehta in forum C Programming
    Replies: 8
    Last Post: 01-09-2013, 08:45 PM
  2. undefined behaviour.
    By juice in forum C Programming
    Replies: 3
    Last Post: 12-21-2011, 01:03 PM
  3. Replies: 5
    Last Post: 04-14-2009, 09:09 PM
  4. Signed Char Overflow
    By coder8137 in forum C Programming
    Replies: 5
    Last Post: 11-17-2006, 08:25 AM
  5. String overflow behaviour
    By Morgan in forum C Programming
    Replies: 15
    Last Post: 10-10-2003, 02:37 AM

Tags for this Thread