Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: SIGFPE crash



On 2011-05-07 15:19:43 -0700, Jon Mayo wrote:
> On Sat, May 7, 2011 at 3:17 PM, Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
> > On 7 May 2011 23:56, Jon Mayo <jon.mayo@xxxxxxxxx> wrote:
> >> echo $[-9223372036854775808/-1]
> >>
> >> this causes zsh to exit immediately with an FPE. Does anyone have a
> >> patch to fix this?
> >
> > diff --git a/Src/math.c b/Src/math.c
> > index 35b362d..3c08052 100644
> > --- a/Src/math.c
> > +++ b/Src/math.c
> > @@ -1053,8 +1053,12 @@ op(int what)
> >                    return;
> >                if (c.type == MN_FLOAT)
> >                    c.u.d = a.u.d / b.u.d;
> > -               else
> > -                   c.u.l = a.u.l / b.u.l;
> > +               else {
> > +                    if (a.u.l == LONG_MIN && b.u.l == -1)
> 
> should be LLONG_MIN
> 
> > +                        c.u.l = 0;
> 
> LLONG_MAX would be the closest answer, but 1 off.

I think this would be very bad. "closest answer" makes sense in
floating-point arithmetic, but on the integers, one can have a
different metric, and zsh doesn't know the choice of the user.

> I would switch it from integer to double type in this case, but that
> might be difficult.

This wouldn't be consistent with the other cases of error, such
as overflows:

$ echo $((-9223372036854775808-1))
9223372036854775807

(the switch to floating-point would have given a different result,
which would be negative) or divisions by 0:

$ echo $((0/0))
zsh: division by zero

with exit status 1, instead of NaN.

IMHO, the best behavior would be an error "division overflow" with
exit status 1.

Note: the modular arithmetic can make sense with addition, subtraction
and multiplication (though an error may be safer), but not with the
division, so that returning LLONG_MIN would not be a good idea, IMHO.
Also, I haven't looked at the zsh code, but if zsh obtains modular
arithmetic by a side effect of the behavior of the processor, then
this is wrong: overflow on signed integer types is undefined behavior
in C (with GCC, you need the -fwrapv option if you want to support
modular arithmetic on signed integer types, otherwise one doesn't know
what optimizations will do).

-- 
Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)



Messages sorted by: Reverse Date, Date, Thread, Author