A strange div bug on Linux x86_64, (both dmd & ldc2): long -5000 / size_t 2 = 9223372036854773308

mw mingwu at gmail.com
Thu Aug 13 22:27:03 UTC 2020


On Thursday, 13 August 2020 at 21:33:11 UTC, Walter Bright wrote:

> To check for overflows, etc., use core.checkedint:
>
> https://dlang.org/phobos/core_checkedint.html
>
> If you're willing to accept some performance reduction, 
> std.experimental.checkedint provides integral types that 
> protect against all kinds of integer arithmetic issues, 
> including "unexpected change of sign":
>
> https://dlang.org/phobos/std_experimental_checkedint.html


So instead of let user change their existing code *manually* all 
over the place, e.g.

     auto r = new int[a.length + b.length];

==>

     auto r = new int[(checked(a.length) + b.length).get];


For users / applications that do value correctness more than 
performance, can we have a compiler switch which turn all the 
types & operations (e.g. in modules, that users also specified on 
command-line) into core_checkedint or std_experimental_checkedint 
*automatically*?



More information about the Digitalmars-d mailing list