[m-rev.] for review: avoid undefined behaviour of int.abs.

Peter Wang novalazy at gmail.com
Mon Jan 15 10:22:53 AEDT 2018


On Sun, 14 Jan 2018 11:56:35 -0500 (EST), Julien Fischer <jfischer at opturion.com> wrote:
> 
> For review by anyone.
> 
> The following is what was discussed back in Oct. 2016, except that I
> haven't added a new exception type specific for overflows (yet).
> 
> I will do something similar for ther other signed integer types once
> this is reviewed adn committed.
> 
> -----------------------
> 
> Avoid undefined behaviour of int.abs.
> 
> library.m:
>      Make int.abs throw an exception for int.min_int.
> 
>      Add the new function unchecked_abs/1, whose behaviour is
>      undefined for int.min_int.
> 
>      Add the new function nabs/1 which computes the negative
>      absolute value of an int   Unlike abs, this is defined for
>      all ints.

Full stop.

> diff --git a/library/int.m b/library/int.m
> index 4dad325..e0b7cd6 100644
> --- a/library/int.m
> +++ b/library/int.m
> @@ -1,7 +1,8 @@
>   %---------------------------------------------------------------------------%
>   % vim: ft=mercury ts=4 sw=4 et
>   %---------------------------------------------------------------------------%
> -% Copyright (C) 1994-2011 The University of Melbourne.
> +% Copyright (C) 1994-2012 The University of Melbourne.
> +% Copyright (C) 2013-2018 The Mercury team.
>   % This file may only be copied under the terms of the GNU Library General
>   % Public License - see the file COPYING.LIB in the Mercury distribution.
>   %---------------------------------------------------------------------------%
> @@ -52,11 +53,24 @@
> 
>   %---------------------------------------------------------------------------%
> 
> -    % Absolute value.
> +    % abs(X) returns the absolute value of X.
> +    % Throws an exception if X = int.min_int.
>       %
>   :- func abs(int) = int.
>   :- pred abs(int::in, int::out) is det.
> 
> +    % unchecked(X) returns the absolute value of X, except that the result is

unchecked_abs

> +    % undefined if X = int.min_int.
> +    %
> +:- func unchecked_abs(int) = int.

The rest is fine.

Peter


More information about the reviews mailing list