Bug 11352 – core.time does not check for overflow when converting integrals to a Duration

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-25T09:26:32Z
Last change time
2017-10-12T03:09:07Z
Assigned to
No Owner
Creator
Andrej Mitrovic

Comments

Comment #0 by andrej.mitrovich — 2013-10-25T09:26:32Z
----- import std.stdio; import core.time; void main() { writeln((ulong.max / 120).seconds); writeln((ulong.max / 60).seconds); } ----- 1016685 weeks, 4 days, 9 hours, 52 minutes, 3 secs, 518 ms, 387 μs, and 2 hnsecs -1016685 weeks, -4 days, -9 hours, -52 minutes, -3 secs, -918 ms, -387 μs, and -2 hnsecs Duration uses a `long` internally, so it should likely try to do some checks before attempting to initialize itself to a ulong.
Comment #1 by issues.dlang — 2017-10-12T03:09:07Z
This is just life with integral types - especially when they're the same size but have different signed-ness. If you want to convert to long from ulong and have it check for overflow, then use std.conv.to!long on the number before constructing a Duration from it. It's quite clear from the signature of dur and its aliases that it takes a long, not a ulong, and always checking for overflow would add overhead for something that happens very rarely. std.conv.to already makes it easy for the programmer to check if they care.