changeset 21121:b625b459cf45

SL: fix division long overflow. Contributed-by: Raffaello Giulietti <raffaello.giulietti@supsi.ch>
author Christian Humer <christian.humer@gmail.com>
date Mon, 27 Apr 2015 17:24:27 +0200
parents c92676a390b8
children 7f78f999512a 6e05dd55d795
files graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/SLDivNode.java
diffstat 1 files changed, 10 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/SLDivNode.java	Mon Apr 27 11:55:51 2015 +0200
+++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/SLDivNode.java	Mon Apr 27 17:24:27 2015 +0200
@@ -41,10 +41,16 @@
         super(src);
     }
 
-    @Specialization
-    protected long div(long left, long right) {
-        /* No overflow is possible on a division. */
-        return left / right;
+    @Specialization(rewriteOn = ArithmeticException.class)
+    protected long div(long left, long right) throws ArithmeticException {
+        long result = left / right;
+        /*
+         * The division overflows if left is Long.MIN_VALUE and right is -1.
+         */
+        if ((left & right & result) < 0) {
+            throw new ArithmeticException("long overflow");
+        }
+        return result;
     }
 
     @Specialization