Repository navigation
fix(exec): capture external child stdout inside builtin pipeline stages (niubash#93) - #102
Merged
Merged
Conversation
…diagnostic Two GNU expr.c-aligned arithmetic fixes: 1. Deferred array subscript evaluation in assignments (expr.c:1395-1401): When the lvalue has an array subscript and the next token is `=`, GNU skips expr_streval and saves the raw token string. The subscript is re-evaluated at bind time (after the RHS), so side effects in the RHS are visible to the subscript. For example, `a[n]=++n` with n=0 stores at a[1] (not a[0]) because ++n increments n before the subscript bind. Adds ArithLValue::IndexedRaw variant that captures the raw subscript text, resolved to Indexed in assign_lvalue after RHS evaluation. 2. Trailing-input diagnostic (expr.c:484-485): `curtok != 0` after EXP_LOWEST() reports "arithmetic syntax error in expression" with the unparsed remainder as the error token. For `(( x=9 y=41 ))` the error token is "y=41 " (trailing blank kept). Uses the raw expression display so trailing blanks survive. Adds ArithmeticErrorCategory::TrailingInput and trailing_input_token() helper that re-parses to find the unparsed remainder. Numeric assignment expressions (e.g. `7 = 43`) are excluded so they keep the "attempted assignment to non-variable" diagnostic. arith suite: 57 → 48 diff lines lib tests: 381 passed; 0 failed Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
In remove_double_quoted_into, `\"` was emitted as a bare `"`, but the expansion walker (expand_embedded_parameters_inner) treats bare `"` as a quote delimiter and toggles in_double, silently dropping it. This made `echo "a\"b"` print `ab` instead of `a"b`, and `recho $"hello, \"world\""` print `hello, world` instead of `hello, "world"`. Emit the data-double-quote marker (\x18) for `\"` inside double quotes, matching the convention already used for `\"` outside quotes. The expansion walker restores \x18 to `"` on output. GNU source: subst.c string_extract_double_quoted keeps `\"` as a literal quote that survives quote removal. nquote suite: 57 → 53 diff lines quote suite: 0 diff lines (clean) lib tests: 381 passed; 0 failed Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
execute_arithmetic_for_command incremented loop_depth before evaluating the init expression but only decremented it when init succeeded. When init failed (e.g. `for ((j=;;))` with empty RHS), loop_depth stayed at 1, so a following `break` was treated as in-loop and silently aborted the rest of the script instead of printing the out-of-loop diagnostic. GNU 5.3 execute_arith_for_command always decrements loop_level after the loop, even on init failure. The previous code matched the Git Bash 5.2 bug; switch to the GNU 5.3 behavior. arith-for suite: 2 → 0 diff lines (clean) lib tests: 381 passed; 0 failed Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…readonly errors Three GNU-aligned fixes in execute_declare_functions: 1. `declare -Fr` (function names + readonly filter) now lists only readonly functions as `declare -fr NAME`, matching GNU declare.def. Previously it printed `declare -f NAME` for all functions. 2. `declare -f -a` / `declare -f -i` / `declare -f -A` / `declare -f -n` are invalid option combinations (GNU declare_invalid_opts rejects att_function combined with att_array/att_assoc/att_integer/att_nameref). Now prints `declare: -X: invalid option` and returns 1 instead of printing the function body. 3. `declare -f +r NAME` on a readonly function now prints `declare: NAME: readonly function` and returns 1 (GNU declare.def:497). errors suite: 31 → 19 diff lines lib tests: 381 passed; 0 failed Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…bstitution In decode_old_style_backtick_source, a backslash before `"` inside backticks (outside double quotes) was preserved as `\"`, but GNU parse.y parse_matched_pair consumes the backslash via LEX_PASSNEXT and adds the next character verbatim. This made recho "`echo \"Hello world!\"`" print `\Hello world!\` instead of `Hello world!`, because the inner echo received `\"Hello world!\"` (escaped quotes) instead of `"Hello world!"` (real quote delimiters). Now `\"` inside backticks becomes `"` (backslash consumed), matching GNU parse_matched_pair behavior. exp suite: 73 → 69 diff lines lib tests: 381 passed; 0 failed Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
GNU setattr.def includes `n` in ATTROPTS ("aAfnp"). For `readonly`,
`-n` sets undo=1, but undoing att_readonly is silently disallowed
(setattr.def lines 174-176: `if (undo && (attribute & att_readonly))
attribute &= ~att_readonly;`). So `readonly -n NAME` is an accepted
no-op that leaves readonly status unchanged.
Rubash previously rejected `-n` as an invalid option, printing
`readonly: -n: invalid option` and returning EX_USAGE. Now `-n` is
accepted in both the variable path (setattr::readonly_with_io) and
the function path (readonly_functions), matching GNU.
errors suite stderr: 129 → 127 diff lines
lib tests: 381 passed; 0 failed
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
GNU execute_cmd.c:4887-4888 sets `special_builtin_failed = 1` when a POSIX special builtin returns an error status > EX_SHERRBASE (256). After the command (execute_cmd.c:1004-1017), if `posixly_correct && interactive == 0 && special_builtin_failed`, the shell exits with the converted status. Rubash previously continued after special builtin errors in POSIX mode, producing extra "after ..." lines that GNU does not emit. This commit ports the `special_builtin_failed` mechanism: - Add `special_builtin_failed: Cell<bool>` to the Executor, reset before each simple command (command_dispatch.rs), checked after dispatch to exit the non-interactive POSIX shell. - `return` outside a function (return.def:74 returns EX_USAGE=258): set the flag in execute_return (pwd_loop_builtins.rs). - `unset` on readonly/invalid-identifier/non-unsettable (set.def:1046 returns EX_UTILERROR=263): return EX_UTILERROR from unset_name, set the flag in execute_unset_with_stderr after builtin_status conversion. - `unset` invalid option (set.def:855 returns EX_USAGE=258): return EX_USAGE_ORIG=258 from parse_unset_options, convert in the executor. - `set` invalid option/option-name (set.def returns EX_USAGE=258): set the flag in execute_set when set_with_io returns EX_USAGE=2. Verified against WSL GNU Bash 5.3.0: - `bash -o posix -c 'return; echo after'` → exit 2, no "after" (was: 0) - `bash -o posix -c 'readonly a=a; unset -v a; echo after'` → exit 1 (was: 0) - `bash -o posix -c 'unset -v a-b; echo after'` → exit 1 (was: 0) - `bash -o posix -c 'set -z; echo after'` → exit 2, no "after" (was: 0) - `bash -o posix -c 'trap "echo bad" SIGNOSIG; echo after'` → exit 0 (unchanged, trap is non-fatal) - Non-POSIX mode: all cases continue as before (unchanged) errors suite: stdout 16 → 13 diff lines; stderr 127 → 127 (formatting) lib tests: 381 passed; 0 failed Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…tution split_shell_words_with_quote_info bypassed lexer quote removal for command substitution bodies, so `$(echo \a)` kept the backslash where GNU removes it. Apply GNU slashify_in_quotes semantics: outside quotes a backslash escapes any character and is removed; inside double quotes it only retains meaning before \, $, `, ", and newline; inside single quotes it is literal. more-exp: 38 -> 28 diff lines, nquote: 39 -> 37, no regressions in exp/rhs-exp/quote/comsub/new-exp/errors. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
expand_assignment_alternate_mut used expand_parameter_word_mut which always
expands with Unquoted context. Inside double quotes, single quotes are data
not delimiters, so `${fox='$foo'}` with foo=bar should expand $foo and keep
the single quotes, yielding 'bar'. Now uses DoubleQuoted context with
decode_double_quotes_in_quoted_parameter_word and unescape_parameter_operator_result,
matching the +/- operator path (subst.c expand_string_for_rhs).
more-exp: 28 -> 26 diff lines, new-exp: no regression (61), all suites stable.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
decode_double_quotes_in_quoted_parameter_word only handled backslash before CBSDQUOTE chars ($ ` " \ newline). Backslash before ordinary chars like \p or \' was preserved, but GNU expand_word_internal with Q_DOUBLE_QUOTES removes it (subst.c ~11678). Added a fallback '\' arm that drops the backslash and keeps only the char. rhs-exp: 6 -> 0, more-exp: 21 -> 19, no regression in other suites. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
GNU only splits on IFS characters produced by expansion, not literal characters in the word. With IFS=+, $x+$y produces one field a+b, not two fields a and b. Marked literal non-whitespace IFS chars with \x1c before expansion so the field splitter can distinguish them from expansion-produced IFS chars (subst.c field splitting with CTLESC). more-exp: 21 -> 19 (a+b vs a b fix), ifs: 0, no regression. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
copy_dollar_paren_word and related span scanners didn't track quote state when matching closing ) of $(...). A ) inside double quotes incorrectly closed the command substitution. Now skips nested $(...) inside double quotes as a unit in command_substitution_spans_whole_word, scan_substitution_spans, and dollar_command_substitution (parse.y xparse_dolparen quote tracking). $(echo "foo$(echo ")")") now produces foo) instead of foo)). more-exp: 21 -> 19, comsub: no regression. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Replaced incomplete safe-char set with faithful implementation of GNU's sh_backslash_quote (flags=3) from lib/sh/shquote.c. Now correctly handles @, =, %, +, # (at position 0), ~ (at position 0 or after :/=), and non-ASCII printable chars. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…ates
braced_alternate_word_values only recognized literal $@/$* but missed
braced modified forms like ${*/}, ${*,,}, ${@/}, ${@,,}. Updated the
narrow check and positional_at check to use ${@/${* prefixes, and added
PF_ASSIGNRHS join+split path for non-whitespace IFS.
exp: 46 -> 37, no regression.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…me pending Three lexer fixes from GNU source: - \c? in $'...' now produces 0x7f (DEL) per chartypes.h TOCTRL, and trailing \c with no operand passes through literally (strtrans.c) - A " inside single quotes carries \x18 data-double-quote marker when the word has content outside single quotes (nquote.tests line 27: $"hello", $"world" quoting) - $name pending_name tracking in remove_double_quoted_into fixed for escaped $ before name chars Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
${#:foo} should return $# (5) since $# is non-empty, not compute length
of parameter "foo". ${#-posparams} should return $# since # is always set.
${#?} (bare, no error word) should return length of $? (1). Added
hash_is_special_param_with_operator guard so ${#-word}/${#+word}/${#=word}/
${#?word} skip the length-prefix route and use operator semantics, while
${#?} (bare) stays as length of $?. Also fixed substring parse for special
parameter names (#, ?, $, !, -, 0).
more-exp: 19 -> 7, no regression.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…$@ scan
Three fixes for ${@}/${*} with operators:
- quoted_positional_at_segments now skips ${...} bodies so "$@" inside
${undef-"$@"} is not treated as top-level positional-at (x${undef-"$@"}
produced x} instead of x)
- quoted_braced_alternate_positional_at_values handles ${@-}/${@:-} when
@ is set: returns one field per positional (like "$@")
- quoted_positional_at_word_values handles -/:-/+/:+ operators for @ and *
in the for-loop word expansion path
more-exp: 11 -> 7, no regression.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
… is active unixwin/niubash#93: a pipeline stage headed by a builtin that transitively spawns an external command leaked the child's stdout to the real process stdout and fed an empty stream downstream: x=$(command find . -maxdepth 1 -type d | wc -l) # printed listing, x=0 eval "find ." | sort # output escaped < <(eval "find ..." | sort) # procsub file empty execute_builtin_pipeline_stage captures builtin output through the thread-local stdout capture (shell_options::begin_stdout_capture) but leaves the stage subshell's stdout_capture field unset. execute_external gated its Stdio::piped() wiring and its wait_with_output readback solely on that field, so a child spawned via `command`/`eval`/`env` inside the stage inherited the terminal. GNU semantics (execute_cmd.c execute_pipeline + redir.c): every pipeline element's fd 1 is the pipe no matter which builtin dispatches the command inside the element's subshell. Fix: treat an active thread-local capture like the field capture at the three external-spawn gates in external_redirects.rs — apply_external_stdout_redirect, external_needs_fd_copy_capture, and write_external_fd_copy_output. Child output then flows through write_default_stdout into the same single capture buffer, preserving fd-1 write order between builtin and external writes inside one stage body. Verified against WSL GNU Bash 5.3.0 (script-file probes): the four issue-93 reproductions now match (x=3, w=3, c=3, no stdout leak), as do eval/env-prefixed first stages and `2>/dev/null` variants. Regression tests: tests/issue93_builtin_stage_external_leak.rs (7 tests, all passing; cli_tests/executor_tests failure counts identical to master baseline — pre-existing environment failures).
caomengxuan666
added a commit
that referenced
this pull request
Sep 15, 2026
fix(env): skip invalid-identifier env names like GNU bash 5.3 (niubash #102)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
Fixes the rubash side of unixwin/niubash#93.
A pipeline stage headed by a builtin that transitively spawns an external command leaked the child's stdout to the real process stdout and fed an empty stream to the next stage:
execute_builtin_pipeline_stage(src/executor/pipeline_stages.rs) captures a builtin stage's stdout via the thread-local capture (shell_options::begin_stdout_capture) but leaves the stage subshell'sstdout_capturefield unset.execute_externalgated both itsStdio::piped()wiring (apply_external_stdout_redirect) and itswait_with_outputreadback (external_needs_fd_copy_capture/write_external_fd_copy_output) solely on that field — so a child spawned viacommand,eval, orenvinside the stage inherited the terminal.GNU semantics (execute_cmd.c
execute_pipeline+ redir.c): every pipeline element's fd 1 is the pipe regardless of which builtin dispatches the command inside the element's subshell.Fix
In
src/executor/external_redirects.rs, treat an active thread-local capture like the field capture at the three external-spawn gates. Child output then flows throughwrite_default_stdoutinto the same single capture buffer — preserving fd-1 write order between builtin and external writes inside one stage body (eval "printf 'first\n'; cat f | wc -l" | cat→first\n3\n).Verification (WSL GNU Bash 5.3.0 script-file baseline)
x=$(find . -maxdepth 1 -type d | wc -l)w=$(command find . -maxdepth 1 -type d | wc -l)< <(eval "find ..." | sort)reader counteval "find ..." | wc -lenv cat f | wc -lNew regression file
tests/issue93_builtin_stage_external_leak.rs(7 tests, all green).cargo test --lib: 381/381.cli_tests/executor_testsfailure counts identical to master baseline (68/107 pre-existing environment failures, e.g. missingtarget/bashdb-cleanfixture in a fresh worktree).