[PATCH 1/1] ZeroDivide bug in float printing

Derek Zhou <[email protected]>
Newsgroups gmane.comp.lang.smalltalk.gnu.general
Message-ID <[email protected]>
To fix 64bit mode integer multiply overflow detection.
This method should be more robust wrt. compiler optimization

_______________________________________________
help-smalltalk mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
ZeroDivide.patch (text/x-diff, 1017 B)
diff --git a/libgst/interp.inl b/libgst/interp.inl
index dbc631b..3583b19 100644
--- a/libgst/interp.inl
+++ b/libgst/interp.inl
@@ -199,15 +199,23 @@ mul_with_check (OOP op1, OOP op2, mst_Boolean *overflow)
   /* This fallback method uses a division to do overflow check */
   else
     {
-      if COMMON ((((uintptr_t) (a | b)) < (1L << (ST_INT_SIZE / 2))
-	          || b == 0
-	          || result / b == a)
-                 && !INT_OVERFLOW (result))
-        return FROM_INT (result);
-      else
-        *overflow = true;
+      intptr_t abs_a = ABS (a);
+      intptr_t abs_b = ABS (b);
+      intptr_t limit = (1L << (ST_INT_SIZE / 2 - 1));
+      if (COMMON ( abs_a < limit && abs_b < limit ))
+	goto legal;
+      if (UNCOMMON (b == 0L))
+	goto legal;
+      if (UNCOMMON (INT_OVERFLOW (result)))
+	goto overflow;
+      if (UNCOMMON (MAX_ST_INT / abs_b < abs_a))
+	goto overflow;
+    legal:
+      return FROM_INT (result);
     }
 
+ overflow:
+  *overflow = true;
   return FROM_INT (0);
 #endif
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.