Skip to content

Transpiler rewrites raw NUL bytes (0x00) in regex literals: match failure before 1.3.12, RegExp.prototype.source still corrupted in 1.3.14 #33930

Open

Description

@wesley83

What version of Bun is running?

1.3.14 (latest release; the older, more severe behavior also reproduced on 1.3.5 — both shown below)

What platform is your computer?

Microsoft Windows 11 Home 10.0.26200 x64 (transpiler behavior, so presumably platform-independent — the sibling #27553 was reported on Darwin)

What steps can reproduce the bug?

The repro file must contain raw NUL bytes (0x00) inside a regex literal, so it can't be pasted directly. gen.js writes it:

// gen.js — writes repro.js whose regex literal contains RAW 0x00 bytes
const fs = require("fs");
const NUL = "\x00";
fs.writeFileSync(
  "repro.js",
  "const re = /" + NUL + "HFMASK(\\d+)" + NUL + "/g;\n" +
  'const s = "\\x00HFMASK0\\x00"; // escape sequences, transpiler-safe\n' +
  'console.log("source codepoints:", [...re.source].map(c => c.codePointAt(0).toString(16)).join(" "));\n' +
  'console.log("source length:", re.source.length);\n' +
  'console.log("matches:", re.exec(s) !== null);\n'
);
node gen.js
node repro.js
bun repro.js

(The repro prints .source as hex codepoints rather than the string itself because GitHub strips the NUL escape sequence from issue bodies — an earlier revision of this issue was mangled exactly that way.)

What is the expected behavior?

Same as Node (v24.18.0): the two raw U+0000 characters are preserved in the pattern — first and last codepoint 0, source length 13 — and the regex matches the NUL-delimited string:

source codepoints: 0 48 46 4d 41 53 4b 28 5c 64 2b 29 0
source length: 13
matches: true

Per EscapeRegExpPattern, RegExp.prototype.source escapes only / and line terminators, so a raw NUL in the source text must come through as one U+0000 character.

What do you see instead?

Bun 1.3.14 — matching now works, but the transpiler has rewritten each raw NUL into the literal 6-character escape text — backslash, u, 0, 0, 0, 0 (codepoints 5c 75 30 30 30 30) — so .source / .toString() no longer round-trip the source text (length 23 instead of 13):

source codepoints: 5c 75 30 30 30 30 48 46 4d 41 53 4b 28 5c 64 2b 29 5c 75 30 30 30 30
source length: 23
matches: true

Bun 1.3.5 — each raw NUL became the literal 6-character text \uFFFD (codepoints 5c 75 46 46 46 44) and matching failed entirely:

source codepoints: 5c 75 46 46 46 44 48 46 4d 41 53 4b 28 5c 64 2b 29 5c 75 46 46 46 44
source length: 23
matches: false

Additional information

Activity

  1. github-actions commented on Jul 10, 2026

    @github-actions
    Contributor

    Found 1 possible duplicate issue:

    1. RegEx unicode mismatch? #26785

    This issue will be automatically closed as a duplicate in 3 days.

    • If your issue is a duplicate, please close it and 👍 the existing issue instead
    • To prevent auto-closure, add a comment or 👎 this comment

    🤖 Generated with Claude Code

  2. robobun commented on Jul 10, 2026

    @robobun
    Collaborator

    ✅ Reproduced on main: the bun-target printer rewrites each raw control character in a regex literal into literal \uXXXX escape text, so re.source.length is 23 instead of 13 and .source no longer round-trips.

    Fix in progress → #33866, which prints the regex pattern text verbatim (covers the raw-NUL case here and the non-ASCII case from #30563). A minimal control-characters-only fix with tests for this repro is also available on the farm/94e20982/regex-literal-control-chars branch.

  3. added 2 commits that reference this issue on Aug 1, 2026
    2a1a2d0
    7614ebe
  4. added 2 commits that reference this issue on Aug 13, 2026
    264b81a
    90fd0c9
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