title: Detecting signed overflow in C/C++
body: <p>At first glance, this question may seem like a duplicate of <a href="https://stackoverflow.com/questions/199333/best-way-to-detect-integer-overflow-in-c-c">How to detect integer overflow?</a>, however it is actually significantly different.</p>
  
  <p>I've found that while detecting an unsigned integer overflow is pretty trivial, detecting a <em>signed</em> overflow in C/C++ is actually more difficult than most people think.</p>
  
  <p>The most obvious, yet naive, way to do it would be something like:</p>
  
  <pre><code>int add(int lhs, int rhs)
  {
   int sum = lhs + rhs;
   if ((lhs &gt;= 0 &amp;&amp; sum &lt; rhs) || (lhs &lt; 0 &amp;&amp; sum &gt; rhs)) {
    /* an overflow has occurred */
    abort();
   }
   return sum; 
  }
  </code></pre>
  
  <p>The problem with this is that according to the C standard, signed integer overflow is <em>undefined behavior.</em>  In other words, according to the standard, as soon as you even cause a signed overflow, your program is just as invalid as if you dereferenced a null pointer.  So you can't cause undefined behavior, and then try to detect the overflow after the fact, as in the above post-condition check example.</p>
  
  <p>Even though the above check is likely to work on many compilers, you can't count on it.  In fact, because the C standard says signed integer overflow is undefined, some compilers (like GCC) will <a href="http://patrakov.blogspot.com/2008_10_01_archive.html" rel="noreferrer">optimize away the above check</a> when optimization flags are set, because the compiler assumes a signed overflow is impossible.  This totally breaks the attempt to check for overflow.</p>
  
  <p>So, another possible way to check for overflow would be:</p>
  
  <pre><code>int add(int lhs, int rhs)
  {
   if (lhs &gt;= 0 &amp;&amp; rhs &gt;= 0) {
    if (INT_MAX - lhs &lt;= rhs) {
     /* overflow has occurred */
     abort();
    }
   }
   else if (lhs &lt; 0 &amp;&amp; rhs &lt; 0) {
    if (lhs &lt;= INT_MIN - rhs) {
     /* overflow has occurred */
     abort();
    }
   }
  
   return lhs + rhs;
  }
  </code></pre>
  
  <p>This seems more promising, since we don't actually add the two integers together until we make sure in advance that performing such an add will not result in overflow.  Thus, we don't cause any undefined behavior.  </p>
  
  <p>However, this solution is unfortunately a lot less efficient than the initial solution, since you have to perform a subtract operation just to test if your addition operation will work.  And even if you don't care about this (small) performance hit, I'm still not entirely convinced this solution is adequate.  The expression <code>lhs &lt;= INT_MIN - rhs</code> seems exactly like the sort of expression the compiler might optimize away, thinking that signed overflow is impossible.  </p>
  
  <p>So is there a better solution here?  Something that is guaranteed to 1) not cause undefined behavior, and 2) not provide the compiler with an opportunity to optimize away overflow checks?  I was thinking there might be some way to do it by casting both operands to unsigned, and performing checks by rolling your own two's-complement arithmetic, but I'm not really sure how to do that.</p>