Skip to content

Commit 4003a1c

Browse files
committed
tighten up narrowing conversion checks
1 parent e526bb8 commit 4003a1c

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/jvm/clojure/lang/Compiler.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -769,21 +769,21 @@ else if(paramType == char.class)
769769
}
770770
else
771771
{
772-
Method m = intValueMethod;
772+
Method m = null;
773773
gen.checkCast(NUMBER_TYPE);
774774
if(paramType == int.class)
775-
m = intValueMethod;
775+
m = Method.getMethod("int intCast(Object)");
776776
else if(paramType == float.class)
777-
m = floatValueMethod;
777+
m = Method.getMethod("float floatCast(Object)");
778778
else if(paramType == double.class)
779-
m = doubleValueMethod;
780-
else if(paramType == long.class)
781-
m = longValueMethod;
782-
else if(paramType == byte.class)
783-
m = byteValueMethod;
784-
else if(paramType == short.class)
785-
m = shortValueMethod;
786-
gen.invokeVirtual(NUMBER_TYPE, m);
779+
m = Method.getMethod("double doubleCast(Object)");
780+
else if(paramType == long.class)
781+
m = Method.getMethod("long longCast(Object)");
782+
else if(paramType == byte.class)
783+
m = Method.getMethod("byte byteCast(Object)");
784+
else if(paramType == short.class)
785+
m = Method.getMethod("short shortCast(Object)");
786+
gen.invokeStatic(RT_TYPE, m);
787787
}
788788
}
789789
else

src/jvm/clojure/lang/RT.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,15 +900,19 @@ static public boolean booleanCast(boolean x){
900900
}
901901

902902
static public byte byteCast(Object x){
903-
long n = ((Number) x).longValue();
903+
if(x instanceof Byte)
904+
return ((Byte) x).byteValue();
905+
long n = longCast(x);
904906
if(n < Byte.MIN_VALUE || n > Byte.MAX_VALUE)
905907
throw new IllegalArgumentException("Value out of range for byte: " + x);
906908

907909
return (byte) n;
908910
}
909911

910912
static public short shortCast(Object x){
911-
long n = ((Number) x).longValue();
913+
if(x instanceof Short)
914+
return ((Short) x).shortValue();
915+
long n = longCast(x);
912916
if(n < Short.MIN_VALUE || n > Short.MAX_VALUE)
913917
throw new IllegalArgumentException("Value out of range for short: " + x);
914918

@@ -919,7 +923,10 @@ static public int intCast(Object x){
919923
if(x instanceof Integer)
920924
return ((Integer)x).intValue();
921925
if(x instanceof Number)
922-
return intCast(((Number) x).longValue());
926+
{
927+
long n = longCast(x);
928+
return intCast(n);
929+
}
923930
return ((Character) x).charValue();
924931
}
925932

@@ -958,6 +965,16 @@ static public int intCast(double x){
958965
}
959966

960967
static public long longCast(Object x){
968+
if(x instanceof Integer || x instanceof Long)
969+
return ((Number) x).longValue();
970+
else if (x instanceof BigInteger)
971+
{
972+
BigInteger bi = (BigInteger) x;
973+
if(bi.bitLength() < 64)
974+
return bi.longValue();
975+
else
976+
throw new IllegalArgumentException("Value out of range for long: " + x);
977+
}
961978
return ((Number) x).longValue();
962979
}
963980

0 commit comments

Comments
 (0)