{"record":{"id":"c9a28e8ab4557524","repo":"perwendel/spark","slug":"s","errorCode":null,"errorMessage":"${s}","messagePattern":"\\$\\{s\\}","errorType":"exception","errorClass":"java.lang.NumberFormatException","httpStatus":null,"severity":"error","filePath":"src/main/java/spark/utils/urldecoding/TypeUtil.java","lineNumber":174,"sourceCode":"     * @param length Length of integer or -1 for remainder of string\n     * @param base   base of the integer\n     * @return the parsed integer\n     * @throws NumberFormatException if the string cannot be parsed\n     */\n    public static int parseInt(String s, int offset, int length, int base)\n        throws NumberFormatException {\n        int value = 0;\n\n        if (length < 0) {\n            length = s.length() - offset;\n        }\n\n        for (int i = 0; i < length; i++) {\n            char c = s.charAt(offset + i);\n\n            int digit = convertHexDigit((int) c);\n            if (digit < 0 || digit >= base) {\n                throw new NumberFormatException(s.substring(offset, offset + length));\n            }\n            value = value * base + digit;\n        }\n        return value;\n    }\n\n    /* ------------------------------------------------------------ */\n    public static String toString(byte[] bytes, int base) {\n        StringBuilder buf = new StringBuilder();\n        for (byte b : bytes) {\n            int bi = 0xff & b;\n            int c = '0' + (bi / base) % base;\n            if (c > '9') {\n                c = 'a' + (c - '0' - 10);\n            }\n            buf.append((char) c);\n            c = '0' + bi % base;\n            if (c > '9') {","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/perwendel/spark/blob/1973e402f5d4c1442ad34a1d38ed0758079f7773/src/main/java/spark/utils/urldecoding/TypeUtil.java#L156-L192","documentation":"TypeUtil.parseInt throws NumberFormatException carrying the offending substring when a character inside the parsed range is not a valid digit for the requested base. It is the low-level hex/integer parser used by URL decoding in the spark utils.","triggerScenarios":"Calling TypeUtil.parseInt(s, offset, length, base) where any character in s.substring(offset, offset+length) is outside the digit range for the given base, e.g. parsing a non-hex character with base 16, or a slice of invalid characters.","commonSituations":"Malformed percent-encoded URLs where the two characters after '%' are not hex digits; corrupted query strings; calling the parser directly with hand-crafted substrings of wrong length.","solutions":["Validate the substring characters are legal digits for the base before calling parseInt.","Catch NumberFormatException around the parse and treat the input as a malformed URL/string.","Sanitize/URL-encode the input at the source so only valid escape sequences arrive.","Use a standard decoder (URLDecoder.decode) for user-supplied strings instead of the raw TypeUtil parser."],"exampleFix":"// before\nlong v = TypeUtil.parseInt(s, 0, 2, 16);\n// after\nlong v;\ntry {\n    v = TypeUtil.parseInt(s, 0, 2, 16);\n} catch (NumberFormatException e) {\n    throw new IllegalArgumentException(\"Invalid hex digits: \" + e.getMessage());\n}","handlingStrategy":"try-catch","validationCode":"boolean validForBase(String s, int offset, int length, int base) {\n    for (int i = 0; i < length; i++) {\n        if (Character.digit(s.charAt(offset + i), base) < 0) return false;\n    }\n    return true;\n}","typeGuard":null,"tryCatchPattern":"try {\n    long v = TypeUtil.parseInt(s, offset, length, base);\n} catch (NumberFormatException e) {\n    throw new IllegalArgumentException(\"Invalid digits: \" + e.getMessage());\n}","preventionTips":["Validate characters against the base before parsing.","Catch NumberFormatException around all TypeUtil parsing calls.","Prefer URLDecoder for user-supplied strings.","Reject malformed escape sequences at the request boundary."],"tags":["numberformat","parsing","url-decoding"],"backgroundTag":"invalid-argument-format","analyzedSha":"1973e402f5d4c1442ad34a1d38ed0758079f7773","analyzedAt":"2026-09-10T14:38:22.866Z","contentChangedAt":"2026-09-10T14:38:22.866Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}