Thread overview
[Issue 5262] New: divide by 0 in std.datetime's Ticks.toMicroseconds()
Nov 24, 2010
Brad Roberts
Nov 24, 2010
Jonathan M Davis
Feb 26, 2011
Jonathan M Davis
November 24, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5262

           Summary: divide by 0 in std.datetime's Ticks.toMicroseconds()
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: braddr@puremagic.com


--- Comment #0 from Brad Roberts <braddr@puremagic.com> 2010-11-23 16:08:41 PST ---
fedora 12 (ami-0d638d64 running on an ec2 t1.micro in case it matters), running the phobos unit tests I'm getting a fp exception.  This is a 64 bit box, 32 bit build of dmd, druntime, and phobos.

Inside the static ctor for struct Ticks, it's hitting the true code path of the static if and false path of if clock_getres():

    ticksPerSec = 1000_000_000 / ts.tv_nsec;

ts.tv_nsec=999848 which results in ticksPerSec=1000.

Later, when this code runs:
    const
    T toMicroseconds(T)() if (isIntegral!T && T.sizeof >= 4)
    {
        return value/(ticksPerSec/1000/1000);
    }

ticksPerSec/1000/1000 --> 0

called from here:
285         unittest
286         {
287             auto t = Ticks.fromMicroseconds(1000000);
288             assert(t.usec == 1000000); // <--- here

On the auto-testers, running ubuntu, it takes the false path of that static if and just directly assigns ticksPerSec to 1M which survives the /1000/1000 as 1.

The full stack:
(gdb) bt
#0  0x0808490c in __ULDIV__ ()
#1  0x0807a952 in std.datetime.Ticks.toMicroseconds!(long).toMicroseconds
(this=0xffffd35c) at std/datetime.d:248
#2  0x0807a31c in std.datetime.Ticks.__unittest6 () at std/datetime.d:284
#3  0x0807bba0 in _D3std8datetime9__modtestFZv ()
#4  0x08080d5c in
_D4core7runtime18runModuleUnitTestsUZb16__foreachbody168MFKPS6object10ModuleInfoZi
()
#5  0x0807dcd2 in _D6object10ModuleInfo7opApplyFMDFKPS6object10ModuleInfoZiZi
()
#6  0x08080c77 in runModuleUnitTests ()
#7  0x0807e448 in _D2rt6dmain24mainUiPPaZi6runAllMFZv ()
#8  0x0807e370 in _D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv ()
#9  0x0807e316 in main ()

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 24, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5262


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #1 from Jonathan M Davis <jmdavisProg@gmx.com> 2010-11-23 20:10:42 PST ---
I'm pretty certain that the fix is to change toMicroseconds to

    T toMicroseconds(T)() if (isIntegral!T && T.sizeof >= 4)
    {
        enum unitsPerSec = 1_000_000;

        if(ticksPerSec >= unitsPerSec)
            return value / (ticksPerSec / unitsPerSec);
        else
            return value * (unitsPerSec / ticksPerSec);
    }

All of the toX() functions should be like this with differing unitsPerSec. However, microseconds is probably the only one where it's actually an issue.

I did a templated version in my datetime code which is essentially the function above, adjusted to take multiple units into account. Once my datetime code has been put into Phobos, replacing the current std.datetime, this bug should be fixed. But the above version of toMicroseconds should fix the problem for now.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 26, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5262


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------