zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: prevent SIGFPE on systems where LONG_MIN < -LONG_MAX
@ 2012-09-03 14:32 Jérémie Roquet
  2012-09-03 14:40 ` Mikael Magnusson
  0 siblings, 1 reply; 7+ messages in thread
From: Jérémie Roquet @ 2012-09-03 14:32 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 217 bytes --]

Hello,

$ echo $[ - 2**63 / -1 ]
zsh: floating point exception  ./Src/zsh

$ echo $[ - 2**63 % -1 ]
zsh: floating point exception  ./Src/zsh

The attached patch fixes this.

Best regards,

-- 
Jérémie

[-- Attachment #2: zsh_fix_sigfpe.patch --]
[-- Type: application/octet-stream, Size: 1072 bytes --]

diff --git a/Src/math.c b/Src/math.c
index cca5210..5e0a509 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -32,6 +32,7 @@ struct mathvalue;
 #include "zsh.mdh"
 #include "math.pro"
 
+#include <limits.h>
 #include <math.h>
 
 /* nonzero means we are not evaluating, just parsing */
@@ -962,6 +963,19 @@ notzero(mnumber a)
     return 1;
 }
 
+/**/
+static int
+representable(mnumber a, mnumber b)
+{
+#if LONG_MIN < -LONG_MAX
+    if (a.u.l - 1 > a.u.l && b.u.l == -1) {
+	zerr("non representable result");
+	return 0;
+    }
+#endif
+    return 1;
+}
+
 /* macro to pop three values off the value stack */
 
 /**/
@@ -1053,13 +1067,18 @@ op(int what)
 		    return;
 		if (c.type == MN_FLOAT)
 		    c.u.d = a.u.d / b.u.d;
-		else
+		else {
+                    if (!representable(a, b))
+                        return;
 		    c.u.l = a.u.l / b.u.l;
+                }
 		break;
 	    case MOD:
 	    case MODEQ:
 		if (!notzero(b))
 		    return;
+                if (!representable(a, b))
+                    return;
 		c.u.l = a.u.l % b.u.l;
 		break;
 	    case PLUS:

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-09-04 23:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-03 14:32 PATCH: prevent SIGFPE on systems where LONG_MIN < -LONG_MAX Jérémie Roquet
2012-09-03 14:40 ` Mikael Magnusson
2012-09-03 14:57   ` Peter Stephenson
2012-09-03 15:08     ` Jérémie Roquet
2012-09-03 16:26       ` Vincent Lefevre
2012-09-04 18:26         ` Peter Stephenson
2012-09-04 23:50           ` Vincent Lefevre

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).