The case for integer overflow checks?

Moritz Maxeiner moritz at ucworks.org
Mon Sep 18 22:39:09 UTC 2017


On Monday, 18 September 2017 at 22:32:28 UTC, Dennis Cote wrote:
> On Monday, 18 September 2017 at 13:25:55 UTC, Andrei 
> Alexandrescu wrote:
>> For the record, with the help of std.experimental.checkedint, 
>> the change that fixes the code would be:
>>
>> malloc(width * height * 4) ==> malloc((checked(width) * height 
>> * 4).get)
>>
>> That aborts the application with a message if a multiplication 
>> overflows.
>
> Can it do something other than abort? Can it throw an overflow 
> exception that could be caught to report the error and continue?

Yes. Use one of the provided hooks (e.g. [1][2][3]) or write one 
that fits your use case.

[1] 
https://dlang.org/phobos/std_experimental_checkedint.html#Abort
[2] 
https://dlang.org/phobos/std_experimental_checkedint.html#Throw
[3] https://dlang.org/phobos/std_experimental_checkedint.html#Warn


More information about the Digitalmars-d mailing list