Skip to content

End ANSI-C strings at the first NUL escape - #17

Merged
webpro merged 2 commits into
webpro-nl:mainfrom
mkantautas:fix/ansi-c-nul
Oct 23, 2026
Merged

webpro merged 2 commits into
webpro-nl:mainfrom
mkantautas:fix/ansi-c-nul

Conversation

@mkantautas

@mkantautas mkantautas commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Bash ends a $'...' string at the first NUL that an escape produces (\0, \x00, \400, \c@, \U00000000). The rest of the quoted text is dropped, and text after the closing quote still joins the word. decodeAnsiCQuoted kept the NUL and everything after it in value:

Source Bash 5.3.20 argument Word.value before after
read -r -d $'\0' file '' "\x00" ""
$'a\0b'c ac "a\x00bc" "ac"
$'\c@x' '' "\x00x" ""
$'\U00000000y' '' "\x00y" ""

read -d $'\0' and mapfile -d $'\0' get the same empty string from Bash as -d ''.

Change

The decoder records where the first escape-produced NUL lands and cuts value there after scanning to the closing quote, so end, text, closed and the rest of the word are unchanged. A \c operand outside ASCII keeps its current decoding.

Tests

New test in test/word-parts.test.ts with values checked against Bash 5.3.20; it fails before the change and passes after. pnpm lint, pnpm test, pnpm test:types and node test/runtime.mjs on Node 14 pass.

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Mantas! I checked this against Bash 5.3 and the new values match for every NUL-producing escape I tried, including \x0, \c followed by a backtick or a space, and a NUL right after pending UTF-8 bytes. The read -r -d $'\0' in git-completion.bash and the $'\0' comparison in acme.sh from our fixtures now get "", as in Bash, and the parse path shows no measurable cost. Heredoc delimiters go through the same decoder, so cat <<$'E\0X' now closes on a line E, as Bash does, instead of swallowing the rest of the script. I pushed a test for that case.

@webpro
webpro merged commit 4e08743 into webpro-nl:main Oct 23, 2026
1 check passed
@webpro

webpro commented Oct 30, 2026

Copy link
Copy Markdown
Member

🚀 This pull request is included in v6.0.2. See Release 6.0.2 for release notes.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants