Skip to content

Commit 7963254

Browse files
committed
reject invalid numbers without throwing exception in Bignteger constructor
1 parent 0882792 commit 7963254

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,9 @@ public abstract static class IntNode extends PythonTernaryBuiltinNode {
11331133
private static Object stringToIntInternal(String num, int base) {
11341134
try {
11351135
BigInteger bi = asciiToBigInteger(num, base);
1136+
if (bi == null) {
1137+
return null;
1138+
}
11361139
if (bi.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0 || bi.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0) {
11371140
return bi;
11381141
} else {
@@ -1267,18 +1270,32 @@ private static BigInteger asciiToBigInteger(String str, int possibleBase) throws
12671270
base = 10;
12681271
}
12691272

1270-
int i = b;
1271-
while (i < e) {
1272-
if (str.charAt(i) == '_') {
1273+
// reject invalid characters without going to BigInteger
1274+
for (int i = b; i < e; i++) {
1275+
char c = str.charAt(i);
1276+
if (c == '_') {
12731277
if (!acceptUnderscore || i == e - 1) {
12741278
throw new NumberFormatException("Illegal underscore in int literal");
12751279
} else {
12761280
acceptUnderscore = false;
12771281
}
12781282
} else {
12791283
acceptUnderscore = true;
1284+
if (base <= 10) {
1285+
if (c < '0' || c > ('0' - 1 + base)) {
1286+
// invalid char
1287+
return null;
1288+
}
1289+
} else {
1290+
if (c < '0' || c > '9') {
1291+
// not in 0-9, check for a-z/A-Z
1292+
if ((c < 'a' || c > ('a' - 11 + base)) && (c < 'A' || c > ('A' - 11 + base))) {
1293+
// invalid char
1294+
return null;
1295+
}
1296+
}
1297+
}
12801298
}
1281-
++i;
12821299
}
12831300

12841301
String s = str;

0 commit comments

Comments
 (0)