Allow Integer.MIN_VALUE to be parsed as an unsigned bin or hex literal (#10720)

* Allow Integer.MIN_VALUE to be parsed in two's complement form

* Removed misleading FIXME comments: Long.MIN_VALUE cannot be parsed at all

* Avoid casting invalidNum representation to double
This commit is contained in:
Cardillan 2025-04-28 18:21:46 +02:00 committed by GitHub
parent b42a5433c5
commit 0fca48b627
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -11,8 +11,8 @@ import mindustry.logic.LExecutor.*;
public class LAssembler{
public static ObjectMap<String, Func<String[], LStatement>> customParsers = new ObjectMap<>();
private static final int invalidNumNegative = Integer.MIN_VALUE;
private static final int invalidNumPositive = Integer.MAX_VALUE;
private static final long invalidNumNegative = Long.MIN_VALUE;
private static final long invalidNumPositive = Long.MAX_VALUE;
private boolean privileged;
/** Maps names to variable. */
@ -74,10 +74,9 @@ public class LAssembler{
symbol = symbol.replace(' ', '_');
//use a positive invalid number if number might be negative, else use a negative invalid number
int usedInvalidNum = symbol.length() > 0 && symbol.charAt(0) == '-' ? invalidNumPositive : invalidNumNegative;
double value = parseDouble(symbol, usedInvalidNum);
double value = parseDouble(symbol);
if(value == usedInvalidNum){
if(Double.isNaN(value)){
return putVar(symbol);
}else{
//this creates a hidden const variable with the specified value
@ -85,23 +84,24 @@ public class LAssembler{
}
}
double parseDouble(String symbol, int invalidNum){
double parseDouble(String symbol){
//parse hex/binary syntax
if(symbol.startsWith("0b")) return Strings.parseLong(symbol, 2, 2, symbol.length(), invalidNum);
if(symbol.startsWith("+0b")) return Strings.parseLong(symbol, 2, 3, symbol.length(), invalidNum);
if(symbol.startsWith("-0b")) return -Strings.parseLong(symbol, 2, 3, symbol.length(), invalidNum);//FIXME: breaks with Long.MIN_VALUE
if(symbol.startsWith("0x")) return Strings.parseLong(symbol, 16, 2, symbol.length(), invalidNum);
if(symbol.startsWith("+0x")) return Strings.parseLong(symbol, 16, 3, symbol.length(), invalidNum);
if(symbol.startsWith("-0x")) return -Strings.parseLong(symbol, 16, 3, symbol.length(), invalidNum);//FIXME: breaks with Long.MIN_VALUE
if(symbol.startsWith("%[") && symbol.endsWith("]") && symbol.length() > 3){
double color = parseNamedColor(symbol);
if(color != -1d){
return color;
}
}
if(symbol.startsWith("0b")) return parseLong(false, symbol, 2, 2, symbol.length());
if(symbol.startsWith("+0b")) return parseLong(false, symbol, 2, 3, symbol.length());
if(symbol.startsWith("-0b")) return parseLong(true,symbol, 2, 3, symbol.length());
if(symbol.startsWith("0x")) return parseLong(false,symbol, 16, 2, symbol.length());
if(symbol.startsWith("+0x")) return parseLong(false,symbol, 16, 3, symbol.length());
if(symbol.startsWith("-0x")) return parseLong(true,symbol, 16, 3, symbol.length());
if(symbol.startsWith("%[") && symbol.endsWith("]") && symbol.length() > 3) return parseNamedColor(symbol);
if(symbol.startsWith("%") && (symbol.length() == 7 || symbol.length() == 9)) return parseColor(symbol);
return Strings.parseDouble(symbol, invalidNum);
return Strings.parseDouble(symbol, Double.NaN);
}
double parseLong(boolean negative, String s, int radix, int start, int end) {
long usedInvalidNum = negative ? invalidNumPositive : invalidNumNegative;
long l = Strings.parseLong(s, radix, start, end, usedInvalidNum);
return l == usedInvalidNum ? Double.NaN : negative ? -l : l;
}
double parseColor(String symbol){
@ -117,7 +117,7 @@ public class LAssembler{
double parseNamedColor(String symbol){
Color color = Colors.get(symbol.substring(2, symbol.length() - 1));
return color == null ? -1d : color.toDoubleBits();
return color == null ? Double.NaN : color.toDoubleBits();
}
/** Adds a constant value by name. */