www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5262] New: divide by 0 in std.datetime's Ticks.toMicroseconds()

reply d-bugmail puremagic.com writes:
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



---
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


(this=0xffffd35c) at std/datetime.d:248



_D4core7runtime18runModuleUnitTestsUZb16__foreachbody168MFKPS6object10ModuleInfoZi
()

()





-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 23 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5262


Jonathan M Davis <jmdavisProg gmx.com> changed:

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



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: -------
Nov 23 2010
prev sibling parent d-bugmail puremagic.com writes:
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: -------
Feb 26 2011