I encountered some corner cases where overflow checking for "-"
doesn't work as I would expect:
user=> (- Integer/MAX_VALUE Integer/MIN_VALUE)
-1
user=> (- Long/MAX_VALUE Long/MIN_VALUE)
-1
The problem seems to be that negating MIN_VALUE yields MIN_VALUE
again, so it slips through the overflow check (see below).
Shall I add that to the issues list?
Kind regards,
achim
src/jvm/clojure/lang/Numbers.java
===================================================================
--- src/jvm/clojure/lang/Numbers.java (revision 1205)
+++ src/jvm/clojure/lang/Numbers.java (working copy)
@@ -1740,7 +1740,7 @@
static public int minus(int x, int y){
int ret = x - y;
- if ((ret ^ x) < 0 && (ret ^ -y) < 0)
+ if (((ret ^ x) < 0 && (ret ^ -y) < 0) || (y == Integer.MIN_VALUE))
return throwIntOverflow();
return ret;
}
@@ -1847,7 +1847,7 @@
static public long minus(long x, long y){
long ret = x - y;
- if ((ret ^ x) < 0 && (ret ^ -y) < 0)
+ if (((ret ^ x) < 0 && (ret ^ -y) < 0) || (y == Long.MIN_VALUE))
return throwIntOverflow();
return ret;
}
I think you should go ahead. I don't see how this could not be a bug.
--Chouser