Skip to content

fix(eval): render the data-double-quote marker back to source before re-parsing - #114

Merged
caomengxuan666 merged 1 commit into
masterfrom
feat/fix-issue124-eval-quote-marker
Sep 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
feat/fix-issue124-eval-quote-marker

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes unixwin/niubash#124.

Symptom

In niubash v1.1.4 an external command that arrives wrapped in eval '<cmd>' receives its inner single-quoted argument as literal characters:

$ niu -c "eval 'd='\''/f/codes/ai/work'\''; mkdir -p \"\$d\"; echo \"mkdir rc=\$?\"'"
mkdir: cannot create directory '"/f/codes/ai/work"': No such file or directory
"mkdir rc=1"
$ bash -c "eval 'd='\''/f/codes/ai/work'\''; mkdir -p \"\$d\"; echo \"mkdir rc=\$?\"'"
mkdir rc=0

Hermes and other agent frameworks wrap every command that way — eval 'source snap; cd; <cmd with '\'' escapes>; write cwd' — so write_file failed 100% of the time. Minimal shape:

eval 'd='\''/x'\''; mkdir -p "$d"'     # GNU: mkdir rc=0   rubash before: mkdir '"...."'

Root cause

eval hands the string_list of its expanded arguments straight back to parse_and_execute (builtins/eval.def:49 → evalstring), so the text is read as shell source a second time. In GNU, the text that reaches that second parse comes out of the single-quote arm of expand_word_internal (subst.c:11882) via:

  • string_extract_single_quoted (subst.c:1088) — raw substring copy, no dequoting;
  • remove_quoted_escapes (subst.c:11902 → 4901 → dequote_escapes at subst.c:4692) — strips only CTLESC-CTLESC and CTLESC-CTLNUL pairs.

So a " written inside single quotes stays a bare quote character and delimits again on the reparse. The '\'' idiom concatenates a "…" segment back into the argument, and that segment's quotes become syntax on the second parse — which is exactly the intended behaviour.

Rubash stores that same character as its data-double-quote marker (\x18) so the expansion walker does not treat it as a delimiter. eval_source_for_reparse already rendered \x17, \x1f and the ANSI-C quote markers back to source, but not \x18. The marker therefore leaked into the reparse text as literal data and the quotes reached the child process verbatim.

Fix

src/executor/execution_misc.rs — restore \x18 to a bare " in eval_source_for_reparse, with a comment recording the GNU call chain above.

Tests

tests/issue124_eval_quote_marker.rs (7 cases). The first four fail without the .replace('\x18', "\"") line and pass with it; the remaining three pin behaviour that never regressed:

case expectation
eval_single_quote_escape_concat_keeps_double_quotes_as_syntax x y / rc=0
eval_quoted_variable_inside_concat_is_one_word one word
eval_quoted_variable_printf_has_no_literal_quotes no literal "
eval_hermes_wrapper_payload_shape wrapper round-trip
fully_single_quoted_eval_argument_still_delimits control
balanced_literal_double_quotes_group_the_inner_word a b
delimiters_inside_eval_text_pair_across_the_boundary rc=0

Verification

  • cargo test --test issue124_eval_quote_marker — with the fix removed: 3 passed; 4 failed; restored: ok. 7 passed.
  • Differential matrix against GNU bash 5.3 (WSL) and Git Bash GNU 5.2 for the issue's shapes and the neighbouring eval/quote shapes: rubash now matches GNU on every in-scope case. Two separate pre-existing gaps surfaced during the sweep and are deliberately not pinned here (unterminated " inside the eval text, and a bare \" inside the eval text); they will be tracked on their own.

…re-parsing

eval hands the string_list of its expanded arguments straight to
parse_and_execute (builtins/eval.def:49 -> evalstring), so the text is read
as shell source a second time. In GNU the text that reaches that second
parse comes out of expand_word_internal's single-quote arm (subst.c:11882)
via string_extract_single_quoted (subst.c:1088) and remove_quoted_escapes
(subst.c:11902 -> 4901 -> dequote_escapes:4692), which only strips
CTLESC-CTLESC and CTLESC-CTLNUL pairs. A `"` written inside single quotes
therefore stays a bare quote character and delimits again on reparse.

Rubash carries that same character as the data-double-quote marker (\x18)
so the expansion walker does not treat it as a delimiter, and
eval_source_for_reparse already rendered \x17, \x1f and the ANSI-C markers
back to source -- but not \x18. The marker survived into the reparse text,
so the quotes became literal data for the inner command:

  eval 'd='\''/x'\''; mkdir -p "$d"'
  mkdir: cannot create directory '"/x"': No such file or directory

That shape is what agent wrappers emit (Hermes escapes every command as
`eval '<cmd with '\'' escapes>'` to source a session snapshot, cd, run, and
write back a cwd marker), so mkdir/write_file and every other external
command failed. niubash #124.

tests/issue124_eval_quote_marker.rs pins the fixed semantics plus the
controls that never regressed (fully single-quoted arguments, balanced
literal quote pairs).
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.

[bug] eval 内单引号转义拼接后,外部命令参数带上字面引号(Hermes 集成实测:write_file 100% 失败)

1 participant