The case for integer overflow checks?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Sep 19 04:13:09 UTC 2017


On Monday, September 18, 2017 22:39:09 Moritz Maxeiner via Digitalmars-d 
wrote:
> 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

Yeah, it's really quite flexible with minimal code. Andrei talked about it
in his dconf 2017 talk:

https://www.youtube.com/watch?v=29h6jGtZD-U

- Jonathan M Davis



More information about the Digitalmars-d mailing list