Skip to content

[soundness] Malformed \x/\u/\U escapes are silently accepted (0 diagnostics) with corrupted string values: "\u{41}" lexes to "41}" #498

Closed

Description

@trikiman

The smallest .hy program that demonstrates it

Tested on latest main @ 643c5b3 (cargo build -p hyb → ./target/debug/hyb check|build). Three one-liners, all accepted with 0 diagnostics:

// repro1.hy — the LX6 rune escape form inside an interpreted string
fn main() {
    s := "\u{41}"
    fmt.println("s=[{s}] len={len(s)}")
}
// repro2.hy — non-hex digit in \x
fn main() {
    s := "\xG1"
    fmt.println("s=[{s}]")
}
// repro3.hy — non-hex digit inside the 4-digit \u
fn main() {
    s := "\u12G4"
    fmt.println("s=[{s}]")
}

hyb build + run (go1.26.9, rust 1.99.0) emits and executes the corrupted values:

repro1 → s=[41}] len=3     (emitted Go: s := "41}")
repro2 → s=[1]             (emitted Go: s := "1")
repro3 → s=[4]             (emitted Go: s := "4")

What the checker should say

HYB-E0002 (reject). Per LX7, interpreted strings use the Go escape set: \u takes exactly four hex digits — \u{…} is LX6 rune-literal syntax, \xG1/\u12G4 are malformed escapes. GUARANTEES.md's one-line claim — "everything the checker cannot prove is named … there is no permissive unknown" — is what fails here: the program is accepted, the value is silently rewritten, and no diagnostic is emitted. The silently-mangled value then flows anywhere a string flows (map keys, comparisons, output), so every downstream result is wrong with no way to see why.

What it actually said

hyb check repro1.hy → OK (0 diagnostics) (verdict accept), same for repro2/repro3. Family members found by the same root cause:

  • "\ud800" (surrogate) and "\U0011FFFF" (above U+10FFFF): char::from_u32 fails → same silent path.
  • Char-literal variants ('\xG', '\u12', '\u{D800}') surface only an accidental unterminated char literal (the closing quote is never consumed), which names the wrong problem.
  • End-of-input mid-escape (e.g. "\x4) does diagnose (unterminated string) — that path is fine.

Root cause

compiler/crates/hy-syntax/src/lexer.rs, escape_char(): the 'x', 'u' (str-mode), 'U', and char::from_u32(...)? arms return None before recording any diagnostic. Both callers act on None under the comment "unknown escape: keep the char verbatim (diag recorded)" / "diagnostic already recorded" — which is only true for the _ => unknown escape arm. The string caller then bumps one char, pushes it verbatim, and keeps lexing: \u{ is swallowed, 41} leaks into the value.

Claimed severity

P2 — claim-precision bug (accepted value should have been named/rejected): accepted program + silently wrong value, no data race.

Anything else

Fix ready: record the diagnostic in every malformed path (tagged tier0_rule LX7/LX6 so the corpus harness can join on it; HYB-E0002 keeps its parse-error slot), keep end-of-input silent so the caller's unterminated-literal diagnostic fires without a duplicate. Corpus pin 61 (REJECT LX7, HYB-E0002) fails on main and passes with the fix; six malformed-case unit tests plus a valid-escape over-rejection guard in hy-syntax/src/lexer.rs. PR follows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions