Bug 5262 – divide by 0 in std.datetime's Ticks.toMicroseconds()
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-11-23T16:08:00Z
Last change time
2011-02-26T06:08:21Z
Assigned to
nobody
Creator
braddr
Comments
Comment #0 by braddr — 2010-11-23T16:08:41Z
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 ()
Comment #1 by issues.dlang — 2010-11-23T20:10:42Z
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.