eo icon indicating copy to clipboard operation
eo copied to clipboard

Parser crashes due to uncaught NumberFormatException from invalid \uXXXX string literal escape

Open claude[bot] opened this issue 5 months ago โ€ข 2 comments

Requested by Yegor Bugayenko ยท Slack thread

Summary

appendUnicode decodes a \uXXXX escape by calling Integer.parseInt(body.substring(cursor, cursor + 4), 16) whenever there are at least 4 characters after the 'u' run. It never validates that those 4 characters are hex digits. The string tokenizer (Tokens.readString, lines 423-462) accepts ANY backslash escape without validating the following characters, so a literal such as "\uZZZZ" tokenizes fine and reaches unescapeBody during emission. parseInt then throws NumberFormatException. Emission happens inside Line.into, which the walker wraps in a try that catches only ParseError (Eo.dispatch lines 293-299; Eo.continueTextBlock 228-234), so the NumberFormatException propagates out and aborts the whole parse instead of producing a positioned error.

Location

eo-parser/src/main/java/org/eolang/parser/Emissions.java lines 342-357.

Buggy code

private static int appendUnicode(
    final StringBuilder out, final String body, final int start
) {
    int cursor = start;
    while (cursor < body.length() && body.charAt(cursor) == 'u') {
        cursor = cursor + 1;
    }
    if (cursor + 4 > body.length()) {
        out.append('\\').append(body, start, body.length());
    } else {
        out.append(
            (char) Integer.parseInt(body.substring(cursor, cursor + 4), 16)
        );
    }
    return cursor + 4;
}

Why it's wrong

The walker is documented to recover from rule violations by catching ParseError and emitting a positioned error (ParseError Javadoc; Eo lines 231/295/619). A malformed unicode escape is a string-content rule violation (R-9.7) that should surface as a positioned parse error, not an unchecked RuntimeException that kills parsing with an opaque message. Reproduced: unescapeBody("\uZZZZ") throws NumberFormatException: For input string: "ZZZZ" under radix 16.

Verifier confirmation: Independently confirmed. Tokens.readString (lines 440-447) consumes a backslash plus the following character with zero hex validation, so a literal like "\uZZZZ" tokenizes cleanly as a STRING Value. During emission, Emissions.openValue's STRING branch (line 134) eagerly evaluates Emissions.unescape(value.raw()) as a synchronous argument, which routes through unescapeBody (line 264) to appendUnicode (line 353): Integer.parseInt(body.substring(cursor, cursor+4), 16) with no check that those four chars are hex digits. I reproduced the exact failure in isolation: both "\uZZZZ" and "\u12GG" throw NumberFormatException: For input string under radix 16. This emission runs inside Eo.dispatch's try (line 294) and Eo.continueTextBlock's try (line 230), both of which catch ONLY ParseError (lines 295, 231); a NumberFormatException is a RuntimeException and therefore propagates out, aborting the entire parse instead of producing the positioned R-9.7 error the walker is contractually documented to emit. No upstream validation of unicode-escape hex digits exists anywhere in the parser package. The only deviation from the report is cosmetic: the checkout lived in the scratchpad clone, not /workspace/eo (which was absent); the code at the cited line numbers matches the claim verbatim.

Expected behavior

A malformed \uXXXX escape (non-hex digits) should raise a positioned ParseError (e.g. "invalid unicode escape") that the walker can catch and report, not an uncaught NumberFormatException that aborts the entire parse.

Suggested fix

Validate the 4 characters are hex before parsing (e.g. wrap parseInt in a check, or catch NumberFormatException) and emit/throw a ParseError with the line/column of the escape; alternatively validate escapes in Tokens.readString so the failure is a ParseError at tokenization time.

claude[bot] avatar Jun 24 '26 08:06 claude[bot]

Hello! I would like to work on this issue :). I am interested in programming language design, and would love to make a contribution here. Thanks!

KaustAbhinand avatar Jun 24 '26 20:06 KaustAbhinand

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.

github-actions[bot] avatar Jul 09 '26 16:07 github-actions[bot]