!1422 fix issue IDFPGR,为 RadixUtil 类中的 decode 方法添加校验

Merge pull request !1422 from shad0wm00n/v5-dev-1225-3
This commit is contained in:
Looly
2025-12-26 01:42:25 +00:00
committed by Gitee
2 changed files with 26 additions and 1 deletions

View File

@@ -92,12 +92,22 @@ public class RadixUtil {
* @return long
*/
public static long decode(String radixs, String encodeStr) {
if (radixs == null || radixs.length() < 2) {
throw new IllegalArgumentException("radixs must contain at least 2 characters");
}
if (encodeStr == null || encodeStr.isEmpty()) {
throw new IllegalArgumentException("encodeStr is null or empty");
}
//目标是多少进制
int rl = radixs.length();
long res = 0L;
for (char c : encodeStr.toCharArray()) {
res = res * rl + radixs.indexOf(c);
int idx = radixs.indexOf(c);
if (idx < 0) {
throw new IllegalArgumentException("Illegal character '" + c + "' for radixs");
}
res = res * rl + idx;
}
return res;
}

View File

@@ -0,0 +1,15 @@
package cn.hutool.core.util;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class RadixUtilTest {
@Test
public void issueIDFPGRTest() {
String radixs = "0123456789ABC"; // base 13
String bad = "1X3"; // 'X' 不在 radix 中
assertThrows(IllegalArgumentException.class, () -> {
RadixUtil.decode(radixs, bad);
});
}
}