D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 22637 - std.conv `to!double` and `parse!double` dont throw on under/overflow
Summary: std.conv `to!double` and `parse!double` dont throw on under/overflow
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All Linux
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2021-12-30 09:30 UTC by basile-z
Modified: 2022-07-12 08:44 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description basile-z 2021-12-30 09:30:19 UTC
The documentation says:

""
 * Throws:
 *     A $(LREF ConvException) if `source` is empty, if no number could be
 *     parsed, or if an overflow occurred.  
""

The following float literal causes libc strtod to signal overflow inerrno:

   "999123254986799969899949354352145897441435999878464164684643513213254364543545634563454199999999999999999999999433212313213119992313453124136468463543543543999999341534654646546464646541341353541999999999965157349999999999320135273486741354354731567431324134999999999999999999999999999999999999999999999135411.9"

But std.conv `to` and `parse` accept it without throwing

---
void main()
{
    import std.conv;

    auto src = "999123254986799969899949354352145897441435999878464164684643513213254364543545634563454199999999999999999999999433212313213119992313453124136468463543543543999999341534654646546464646541341353541999999999965157349999999999320135273486741354354731567431324134999999999999999999999999999999999999999999999135411.9";
    auto cv1 = parse!double(src);
    auto cv2 = to!double("999123254986799969899949354352145897441435999878464164684643513213254364543545634563454199999999999999999999999433212313213119992313453124136468463543543543999999341534654646546464646541341353541999999999965157349999999999320135273486741354354731567431324134999999999999999999999999999999999999999999999135411.9");
}   
---
Comment 1 Grim Maple 2022-07-08 16:19:46 UTC
I can't confirm this bug on my end. It throws `std.conv.ConvException@C:\LDC\bin\..\import\std\conv.d(3419): Range error` for me.
LDC - the LLVM D compiler (1.29.0):
  based on DMD v2.099.1 and LLVM 13.0.1
Comment 2 Paul Backus 2022-07-08 16:38:46 UTC
I was able to reproduce the bug with DMD 2.100.0 and LDC 1.29.0 on Linux.

From the backslashes in the path, it appears that the failed reproduction attempt was on Windows, so it's possible this bug only occurs on Linux.
Comment 3 Dlang Bot 2022-07-08 18:27:19 UTC
@GrimMaple created dlang/phobos pull request #8502 "Fix Issue 22637 - std.conv `to!double` and `parse!double` dont throw on under/overflow" fixing this issue:

- Fix Issue 22637 - std.conv `to!double` and `parse!double` dont throw on under/overflow
  
  This happened because `parse` enforced on real.
  And provided number actually fits real.
  Changed to enforce on `Target` type instead

https://github.com/dlang/phobos/pull/8502
Comment 4 Dlang Bot 2022-07-12 08:44:20 UTC
dlang/phobos pull request #8502 "Fix Issue 22637 - std.conv `to!double` and `parse!double` dont throw on under/overflow" was merged into master:

- 1c3903c2e644d55913708b6867e33aab1a076ac9 by Grim Maple:
  Fix Issue 22637 - std.conv `to!double` and `parse!double` dont throw on under/overflow
  
  This happened because `parse` enforced on real.
  And provided number actually fits real.
  Changed to enforce on `Target` type instead

https://github.com/dlang/phobos/pull/8502