www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14870] New: incorrect use of assert to detect environmental

https://issues.dlang.org/show_bug.cgi?id=14870

          Issue ID: 14870
           Summary: incorrect use of assert to detect environmental errors
                    in core.time
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: bugzilla digitalmars.com

Note the use of asserts in the following code in core.time:
----
static  property MonoTimeImpl currTime()  trusted nothrow  nogc
{
    if(ticksPerSecond == 0)
    {
        assert(0, "MonoTimeImpl!(ClockType." ~ _clockName ~
                  ") failed to get the frequency of the system's monotonic
clock.");
    }

    version(Windows)
    {
        long ticks;
        if(QueryPerformanceCounter(&ticks) == 0)
        {
            // This probably cannot happen on Windows 95 or later
            assert(0, "Call to QueryPerformanceCounter failed.");
        }
        return MonoTimeImpl(ticks);
    }
    else version(OSX)
        return MonoTimeImpl(mach_absolute_time());
    else version(Posix)
    {
        timespec ts;
        if(clock_gettime(clockArg, &ts) != 0)
            assert(0, "Call to clock_gettime failed.");

        return MonoTimeImpl(convClockFreq(ts.tv_sec * 1_000_000_000L +
ts.tv_nsec,
                                          1_000_000_000L,
                                          ticksPerSecond));
    }
}
---
The user should never see assert failures. But apparently, these are happening
on some systems. Environment issues should not be tested with assert's.
Instead, the issue should be tested for, and if it fails, an error message
printed and the program exited with an error status.

--
Aug 04 2015