{"record":{"id":"5301acea926d63f0","repo":"prestodb/presto","slug":"binary-literal-can-only-contain-hexadecimal-digits","errorCode":null,"errorMessage":"Binary literal can only contain hexadecimal digits","messagePattern":"Binary literal can only contain hexadecimal digits","errorType":"exception","errorClass":"ParsingException","httpStatus":null,"severity":"error","filePath":"presto-parser/src/main/java/com/facebook/presto/sql/tree/BinaryLiteral.java","lineNumber":47,"sourceCode":"{\n    // the grammar could possibly include whitespace in the value it passes to us\n    private static final Pattern WHITESPACE_PATTERN = Pattern.compile(\"[ \\\\r\\\\n\\\\t]\");\n    private static final Pattern NOT_HEX_DIGIT_PATTERN = Pattern.compile(\".*[^A-F0-9].*\");\n\n    private final Slice value;\n\n    public BinaryLiteral(String value)\n    {\n        this(Optional.empty(), value);\n    }\n\n    public BinaryLiteral(Optional<NodeLocation> location, String value)\n    {\n        super(location);\n        requireNonNull(value, \"value is null\");\n        String hexString = WHITESPACE_PATTERN.matcher(value).replaceAll(\"\").toUpperCase();\n        if (NOT_HEX_DIGIT_PATTERN.matcher(hexString).matches()) {\n            throw new ParsingException(\"Binary literal can only contain hexadecimal digits\", location.get());\n        }\n        if (hexString.length() % 2 != 0) {\n            throw new ParsingException(\"Binary literal must contain an even number of digits\", location.get());\n        }\n        this.value = Slices.wrappedBuffer(BaseEncoding.base16().decode(hexString));\n    }\n\n    public BinaryLiteral(NodeLocation location, String value)\n    {\n        this(Optional.of(location), value);\n    }\n\n    /**\n     * Return the valued as a hex-formatted string with upper-case characters\n     */\n    public String toHexString()\n    {\n        return BaseEncoding.base16().encode(value.getBytes());","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-parser/src/main/java/com/facebook/presto/sql/tree/BinaryLiteral.java#L29-L65","documentation":"BinaryLiteral parses X'..' binary string literals by stripping whitespace and hex-decoding the value. If, after removing whitespace, any character is not a hexadecimal digit, the constructor throws this ParsingException because the literal cannot represent bytes.","triggerScenarios":"new BinaryLiteral(location, value) or parsing X'...' where the content includes characters other than 0-9a-fA-F, e.g. X'GG' or X'12 3Z'.","commonSituations":"Copy-pasted binary data containing '0x' prefix or stray characters, hand-typed hex with typos, base64 or decimal bytes mistakenly used as hex.","solutions":["Ensure every character is a hex digit (0-9, A-F)","Remove any '0x' prefix from the literal content","Validate the string with a regex ^[0-9A-Fa-f\\s]*$ before constructing"],"exampleFix":"// before\nnew BinaryLiteral(location, \"0x1A2B\"); // contains 'x'\n// after\nnew BinaryLiteral(location, \"1A2B\");","handlingStrategy":"validation","validationCode":"String hex = value.replaceAll(\"\\\\s\", \"\").toUpperCase();\nif (!hex.matches(\"[0-9A-F]*\")) {\n    throw new IllegalArgumentException(\"Binary literal must contain only hex digits: \" + value);\n}","typeGuard":"boolean isHexLiteral(String value) {\n    return value != null && value.replaceAll(\"\\\\s\", \"\").matches(\"[0-9A-Fa-f]+\");\n}","tryCatchPattern":"try {\n    BinaryLiteral lit = new BinaryLiteral(location, value);\n} catch (ParsingException e) {\n    if (e.getMessage().contains(\"hexadecimal digits\")) {\n        throw new MalformedBinaryLiteralException(value, e);\n    }\n    throw e;\n}","preventionTips":["Strip '0x' prefixes before building X'..' literals","Validate hex with a regex at the input boundary","Only accept hex from trusted encoders (e.g. Guava Base16)","Normalize case and whitespace centrally in one helper"],"tags":["sql","literal","binary","hex"],"backgroundTag":"invalid-literal-value","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}