From: Vincent Lefevre <vincent@vinc17.net>
To: zsh-workers@zsh.org
Subject: Re: SIGFPE crash
Date: Mon, 9 May 2011 12:35:53 +0200 [thread overview]
Message-ID: <20110509103553.GL5625@prunille.vinc17.org> (raw)
In-Reply-To: <BANLkTimtXiG_RNbBxOXKwGMiXoNp_ui1HA@mail.gmail.com>
On 2011-05-07 15:19:43 -0700, Jon Mayo wrote:
> On Sat, May 7, 2011 at 3:17 PM, Mikael Magnusson <mikachu@gmail.com> wrote:
> > On 7 May 2011 23:56, Jon Mayo <jon.mayo@gmail.com> 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@vinc17.net> - 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)
prev parent reply other threads:[~2011-05-09 10:35 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-07 21:56 Jon Mayo
2011-05-07 22:17 ` Mikael Magnusson
2011-05-07 22:19 ` Jon Mayo
2011-05-07 22:27 ` Mikael Magnusson
2011-05-07 23:48 ` Phil Pennock
2011-05-08 0:05 ` Jon Mayo
2011-05-08 0:10 ` Mikael Magnusson
2011-05-08 2:46 ` Bart Schaefer
2011-05-08 7:03 ` Jon Mayo
2011-05-08 17:33 ` Peter Stephenson
2011-05-09 10:21 ` Vincent Lefevre
2011-05-09 10:35 ` Vincent Lefevre [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20110509103553.GL5625@prunille.vinc17.org \
--to=vincent@vinc17.net \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).