[Boost-bugs] [Boost C++ Libraries] #6361: integer overflow in boost::chrono::process_real_cpu_clock::now() under Windows 32bits

Subject: [Boost-bugs] [Boost C++ Libraries] #6361: integer overflow in boost::chrono::process_real_cpu_clock::now() under Windows 32bits
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2012-01-05 17:27:06


#6361: integer overflow in boost::chrono::process_real_cpu_clock::now() under
Windows 32bits
---------------------------------+------------------------------------------
 Reporter: ivan.lelann@… | Owner: viboes
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: chrono
  Version: Boost 1.48.0 | Severity: Problem
 Keywords: |
---------------------------------+------------------------------------------
 As of 1.48 Boost.Chrono contains code below for
 boost::chrono::process_real_cpu_clock::now()
 (boost\chrono\detail\inlined\win\process_cpu_clocks.hpp)

     clock_t c = ::clock();
     /* ... */
     return time_point(
       duration(c*(1000000000l/CLOCKS_PER_SEC))
     );

 duration::rep is int64/nanoseconds, clock_t is long. This is under VS2008,
 Win XP 32bits.
 I think "c" should be cast to duration::rep before being multiplied.

 C-style cast fixed the problem for me.
 Howard Hinnant suggested following cleaner fix on the mailing list :

 typedef ratio_divide<giga, ratio<CLOCKS_PER_SEC>>::type R;
 return time_point(
   duration(static_cast<rep>(c)*R::num/R::den)
 );

 NB: This appears at least twice in this file. It also appears in "mac" and
 "posix" folders though I cannot say if it is problematic on this
 platforms.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/6361>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:08 UTC