Boost C++ Libraries: Ticket #3471: microseconds constructor overflows https://svn.boost.org/trac10/ticket/3471 <p> From the users ML: <a class="ext-link" href="http://lists.boost.org/boost-users/2008/10/41763.php"><span class="icon">​</span>http://lists.boost.org/boost-users/2008/10/41763.php</a> </p> <p> The boost::date_time::microseconds(...) constructor overflows the int64 size limits in intermediary calculations. The code below will reproduce the error (using VC++ 2008 and boost 1.36). </p> <pre class="wiki">ptime start(boost::gregorian::date(2000, 1, 1)); ptime end(boost::gregorian::date(2000, 5, 1)); time_duration td = end - start; ptime wrongEnd = start + microseconds(td.total_microseconds()); BOOST_CHECK_EQUAL(end, wrongEnd); </pre><p> The microseconds constructor takes an int64 as input and the total_microseconds is well within its range. Inside the date_time::subsecond_duration&lt;time_duration, 1000000&gt; the internal duration is calculated as </p> <pre class="wiki">ss*traits_type::res_adjust()/frac_of_second </pre><p> Since the both res_adjust() and frac_of_second have the value 1000000, the result should be ss*1 but... the intermediary value ss*res_adjust overflows the int64 causing a negative number to be returned. </p> <p> Question is, is this by design or is it a bug? I'm rather new to boost::date_time and was hoping to replace our current datetime types with it. We are using millisecond timestamped data over long time series (years). </p> <p> I guess the simple solution would be to add parentheses around (res_adjust()/frac_of_second) assuming frac_of_second always divides res_adjust. However, there are similar issues when adding 200 years worth of seconds which overflows the int32 argument (but that's not of my immediate concern). </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3471 Trac 1.4.3 anonymous Wed, 21 Oct 2009 12:43:33 GMT <link>https://svn.boost.org/trac10/ticket/3471#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3471#comment:1</guid> <description> <p> In my opinion, this is definitely a bug. I'd take it one step further: the presence of this simple integer-overflow bug suggests that there may be integer arithmetic bugs throughout the date_time library. An expert in integer arithmetic should review the entire date_time library looking for similar bugs. </p> <p> Note that the suggested solution of adding parentheses won't work in many common cases, such as when this function is called from the nanoseconds constructor. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Mon, 03 May 2010 02:38:40 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/3471#comment:2 https://svn.boost.org/trac10/ticket/3471#comment:2 <ul> <li><strong>owner</strong> changed from <span class="trac-author">az_sw_dude</span> to <span class="trac-author-anonymous">anonymous</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket anonymous Mon, 03 May 2010 02:39:10 GMT milestone changed https://svn.boost.org/trac10/ticket/3471#comment:3 https://svn.boost.org/trac10/ticket/3471#comment:3 <ul> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.41.0</span> → <span class="trac-field-new">Boost 1.43.0</span> </li> </ul> Ticket anonymous Mon, 03 May 2010 09:18:39 GMT owner, status changed https://svn.boost.org/trac10/ticket/3471#comment:4 https://svn.boost.org/trac10/ticket/3471#comment:4 <ul> <li><strong>owner</strong> changed from <span class="trac-author-anonymous">anonymous</span> to <span class="trac-author">az_sw_dude</span> </li> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">new</span> </li> </ul> Ticket anonymous Mon, 03 May 2010 09:46:59 GMT owner changed https://svn.boost.org/trac10/ticket/3471#comment:5 https://svn.boost.org/trac10/ticket/3471#comment:5 <ul> <li><strong>owner</strong> changed from <span class="trac-author">az_sw_dude</span> to <span class="trac-author">Andrey Semashev</span> </li> </ul> Ticket anonymous Mon, 03 May 2010 11:45:31 GMT attachment set https://svn.boost.org/trac10/ticket/3471 https://svn.boost.org/trac10/ticket/3471 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">subsecond_duration-overflow-fix.diff</span> </li> </ul> Ticket johanditmar@… Tue, 18 May 2010 12:34:04 GMT <link>https://svn.boost.org/trac10/ticket/3471#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3471#comment:6</guid> <description> <p> Any estimate when this will make it into a release? Milestone is still set for 1.43. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Mon, 29 Nov 2010 17:15:23 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3471#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3471#comment:7</guid> <description> <p> Or something like "(ss / frac_of_second) * traits_type::res_adjust() + (ss % frac_of_second) * traits_type::res_adjust() / frac_of_second" ? </p> </description> <category>Ticket</category> </item> <item> <author>Vicente Botet <vicente.botet@…></author> <pubDate>Wed, 08 Dec 2010 06:13:01 GMT</pubDate> <title>type changed https://svn.boost.org/trac10/ticket/3471#comment:8 https://svn.boost.org/trac10/ticket/3471#comment:8 <ul> <li><strong>type</strong> <span class="trac-field-old">Bugs</span> → <span class="trac-field-new">Patches</span> </li> </ul> Ticket Jon Bartosch <jon@…> Fri, 05 Oct 2012 21:42:20 GMT <link>https://svn.boost.org/trac10/ticket/3471#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3471#comment:9</guid> <description> <p> So, this is a real problem. Will this ever make it into a release? It's as simple as a set of parenthesis. If not, can we have some rational in the bug? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Sat, 06 Oct 2012 17:34:08 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3471#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3471#comment:10</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/80887" title="Refs #3471. Precompute the duration conversion coefficient to avoid ...">[80887]</a>) Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3471" title="#3471: Patches: microseconds constructor overflows (closed: fixed)">#3471</a>. Precompute the duration conversion coefficient to avoid integer overflow. Added a test for integer overflow in the subsecond duration constructor. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Wed, 10 Oct 2012 19:05:06 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3471#comment:11 https://svn.boost.org/trac10/ticket/3471#comment:11 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/80940" title="Merged changes from trunk. Fixes #3471.">[80940]</a>) Merged changes from trunk. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3471" title="#3471: Patches: microseconds constructor overflows (closed: fixed)">#3471</a>. </p> Ticket