Boost C++ Libraries: Ticket #650: Invalid code used for add w/ overflow checking https://svn.boost.org/trac10/ticket/650 <pre class="wiki">Discovered while working on our (Microsoft) C++ compiler: This is an e-mail thread about the bug we ran into. The current Microsoft VC++ compiler does not causes this to return incorrect values, but there is no guarantee that this will be the case in the future as we produce better optimizations. If you need to contact me about this, my e-mail is alex.thaman@microsoft.com. &gt; FYI - this bug exists in at least versions of boost &gt;= 1.27.0 &gt; (including the current one). Please see below for details. It's a &gt; little unclear to us what the purpose of this function is, but it &gt; appears to be doing a signed add and handling overflow conditions, but &gt; it's doing to overflow detection incorrectly. &gt; &gt; Thanks, &gt; -Alex Thaman (Microsoft) &gt; &gt; _____________________________________________ &gt; From: Russell Hadley &gt; Sent: Friday, June 09, 2006 3:00 PM &gt; To: Alex Thaman; Arjun Bijanki; Andy Rich &gt; Subject: RE: boost bug &gt; &gt; Just a slight spin on this: For the current boost sources we aren't &gt; making a transformation in the compiler which exposes the reliance on &gt; signed arithmetic overflow/underflow - this was exposed in an older &gt; version - but potentially we could. It is a much safer practice to &gt; test the ranges of the inputs to an arithmetic expression if there is &gt; a danger of overflow rather than the output. The compiler can &gt; potentially reorder any signed integer arithmetic (algebraically) with &gt; out respect for signed overflow/underflow so outputs have undefined &gt; semantics for that case. (off the cuff ex: cast the inputs to &gt; unsigned and check their ranges.) &gt; &gt; Thanks. &gt; &gt; -R &gt; &gt; _____________________________________________ &gt; From: Alex Thaman &gt; Sent: Friday, June 09, 2006 1:53 PM &gt; To: Arjun Bijanki &gt; Cc: Russell Hadley &gt; Subject: boost bug &gt; &gt; We ran into a bug in boost. In &gt; conformance\3rdPartyLibs\boost_1_2*_0 \boost\random\detail\const_mod.hp &gt; p, there is a function called add_signed (do_add in version &gt; 1.27.0). &gt; It has the following code: &gt; &gt; template&lt;&gt; &gt; struct do_add&lt;true&gt; &gt; { &gt; template&lt;class IntType&gt; &gt; static IntType add(IntType m, IntType x, IntType c) &gt; { &gt; x += (c-m); &gt; if(x &lt; 0) &gt; x += m; &gt; return x; &gt; } &gt; }; &gt; &gt; The problem is that it is relying on an overflow in their logic to &gt; calculate the correct value. This is undefined in most cases, and the &gt; optimizer does some logic here by calculating intermediate values and &gt; using them and avoiding the overflow that this test is expecting. It &gt; is pretty clear that they depend on overflow behavior if you just do a &gt; quick refactor of this code: &gt; &gt; If (x + (c - m) &lt; 0) return x + c &gt; Else return x + c - m &gt; &gt; Thanks, &gt; -Alex </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/650 Trac 1.4.3 jmaurer Thu, 29 Jun 2006 23:19:09 GMT <link>https://svn.boost.org/trac10/ticket/650#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/650#comment:1</guid> <description> <pre class="wiki">Logged In: YES user_id=53943 Thanks for the report, sorry for the delay. I've checked in this patch which should fix the issue. --- const_mod.hpp 27 Jul 2004 03:43:32 -0000 1.8 +++ const_mod.hpp 29 Jun 2006 23:15:39 -0000 @@ -43,10 +43,10 @@ template&lt;class IntType&gt; static IntType add(IntType m, IntType x, IntType c) { - x += (c-m); - if(x &lt; 0) - x += m; - return x; + if (x &lt; m - c) + return x + c; + else + return x - (m-c); } }; </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>jmaurer</dc:creator> <pubDate>Thu, 29 Jun 2006 23:19:10 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/650#comment:2 https://svn.boost.org/trac10/ticket/650#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> </ul> Ticket Cialis buy online Mon, 08 Jun 2009 11:20:17 GMT severity set https://svn.boost.org/trac10/ticket/650#comment:3 https://svn.boost.org/trac10/ticket/650#comment:3 <ul> <li><strong>severity</strong> → <span class="trac-field-new">Showstopper</span> </li> </ul> <p> Is it NOUVELLE CUISINE when 3 olives are struggling with a scallop in a plate of SAUCE MORNAY? <a class="ext-link" href="http://www.umabest.com/forums/showthread.php?p=74050"><span class="icon">​</span>comprare viagra dall'Italia</a> <a class="ext-link" href="http://www.gomedia.us/forum/member.php?u=1621"><span class="icon">​</span>fioricet uses</a> <a class="ext-link" href="http://us.cyworld.com/buybrandcialis/"><span class="icon">​</span>buy Cialis medication</a> Tq1iKUm <a class="ext-link" href="http://www.maclife.com/user/buy_tramadol_0"><span class="icon">​</span>cheap tramadol</a> </p> Ticket cialis rx Sat, 08 Aug 2009 13:46:16 GMT <link>https://svn.boost.org/trac10/ticket/650#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/650#comment:4</guid> <description> <p> Apples have meant trouble since eden. </p> <blockquote> <p> -- <a class="missing wiki">MaDsen</a> Wikholm, mwikholm@… </p> </blockquote> <p> <a class="ext-link" href="http://members.ebay.it/ws/eBayISAPI.dll?ViewUserPage&amp;userid=compra-viagra-cialis"><span class="icon">​</span>viagra ordina adesso in italia</a> <a class="ext-link" href="http://headachetreatment.net"><span class="icon">​</span>buy fioricet pills</a> <a class="ext-link" href="http://members.ebay.com/ws/eBayISAPI.dll?ViewUserPage&amp;userid=cialis-levitra"><span class="icon">​</span>cialis</a> <a class="ext-link" href="http://subscene.com/members/Buy-Viagra.aspx"><span class="icon">​</span>buy viagra</a> <a class="ext-link" href="http://buycialis.cc/it/item/generic_propecia.html"><span class="icon">​</span>ordina propecia</a> TQVo/7c </p> </description> <category>Ticket</category> </item> </channel> </rss>