Skip to content

fix(lexer): raw-byte tag a word-initial data ESC (niubash#200) - #474

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/esc-quote-strip-200
Oct 9, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/esc-quote-strip-200

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Tracked by unixwin/niubash#200.

Problem

A raw ESC byte (0x1B) at the start of a word's data - most visibly as the
first character inside single quotes in a script file - was silently
dropped:

printf '%s' '<ESC>[31mAAA<ESC>[0m'   # niu printed: [31mAAA<ESC>[0m

The ESC is byte-identical to QUOTED_WORD_PREFIX (U+001B), the quoted-
literal-tilde marker the lexer itself emits; executor consumers answer
strip_prefix(QUOTED_WORD_PREFIX) and claimed the data byte. Mid-word
ESC bytes survived because the strip only fires at position 0. GNU bash
keeps a raw ESC in every position of every quoting context (verified
against GNU bash 5.2.37 with od -c).

Fix

tag_word_initial_raw_esc at both dequote choke points
(word_value_from_raw, finish_word_token): a leading data ESC is
re-encoded as the U+E000 raw-byte marker pair - the same carrier
push_ansi_c_byte already uses for 0x1B - so no marker consumer can
claim it; the output boundary decodes it back. The quoted-tilde marker
emission is untouched; the textual $'\033' form already decoded to
that carrier and stays byte-identical.

Tests

  • new lexer unit tests: sq/dq/unquoted word-initial raw ESC tagged;
    mid-word ESC stays verbatim; $'<raw-ESC>' tagged and equal to
    $'\033'; quoted-literal-tilde marker still emitted
  • cargo test --lib: 630 passed (2 pre-existing local-env failures in
    pwd/path drive-form tests, failing on clean origin/master too)
  • niu built against this branch matches GNU od -c output for
    single/double/unquoted/$'...' script cases

A raw ESC (U+001B) that reaches a word value as data - source bytes inside
single/double quotes or an unquoted word, or a literal ESC character inside
a $'...' span - is byte-identical to QUOTED_WORD_PREFIX, the quoted-literal-
tilde marker the lexer itself emits. Downstream consumers answer
strip_prefix(QUOTED_WORD_PREFIX), so a word-initial data ESC was silently
eaten: printf '%s' '<ESC>[31mAAA<ESC>[0m' lost the leading ESC while the
mid-string one survived (GNU bash keeps a raw ESC in every position of
every quoting context).

Tag a leading data ESC as the U+E000 raw-byte marker pair at both dequote
choke points (word_value_from_raw and finish_word_token) - the same carrier
ANSI-C decoding already uses for 0x1B - so no marker consumer can claim it;
the output boundary decodes it back to the ESC byte. A mid-word ESC is never
claimed (strip_prefix only fires at position 0) and stays verbatim. The
quoted-tilde marker emission itself is untouched, and the textual $'\033'
form already decoded to the same carrier, so it is behavior-identical.

Verified byte-for-byte against GNU bash 5.2 (od -c) for single-quoted,
double-quoted, unquoted, and $'...' words, plus the input-bridge boundary:
this is lexer text-layer handling of script-file bytes, distinct from the
niubash#434/#451 input-bridge normalization of interactive input.
@caomengxuan666
caomengxuan666 merged commit 7cda2ae into master Oct 9, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/esc-quote-strip-200 branch October 9, 2026 15:34
caomengxuan666 added a commit to unixwin/niubash that referenced this pull request Oct 9, 2026
 #200) (#235)

A raw ESC byte (0x1B) as the first character inside single quotes in a
script file was silently dropped by the parser, while the same byte
mid-string survived; GNU bash keeps a raw ESC in every position of every
quoting context. The root cause and fix live in the rubash lexer
(unixwin/rubash#474): a word-initial data ESC is byte-identical to the
QUOTED_WORD_PREFIX marker and was claimed by downstream strip_prefix
consumers; the lexer now raw-byte tags it.

These integration tests pin the product behavior byte-for-byte at the niu
boundary: the issue's exact single-quote reproducer, word-initial ESC in
single/double-quoted and unquoted words, ANSI-C $'\033' and mid-word
spellings, the assignment storage roundtrip, and the piped-stdin entry
(the niubash#434/#451 input-bridge normalization boundary - this issue is
lexer text-layer handling of script bytes, not bridge normalization).
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.

1 participant