From 0fca48b627c3776d613103c95b71dc61918bc0fc Mon Sep 17 00:00:00 2001 From: Cardillan <122014763+cardillan@users.noreply.github.com> Date: Mon, 28 Apr 2025 18:21:46 +0200 Subject: [PATCH] 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 --- core/src/mindustry/logic/LAssembler.java | 40 ++++++++++++------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/core/src/mindustry/logic/LAssembler.java b/core/src/mindustry/logic/LAssembler.java index d8ca04079b..9ef5a75311 100644 --- a/core/src/mindustry/logic/LAssembler.java +++ b/core/src/mindustry/logic/LAssembler.java @@ -11,8 +11,8 @@ import mindustry.logic.LExecutor.*; public class LAssembler{ public static ObjectMap> 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. */