You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[bug] set -u: array-subscript expansion nested in ${var#pattern} falsely reports 'unbound variable' (kills bash-completion _comp_initialize -n : class) #311
Under set -u (nounset), rubash raises a false "unbound variable" error for an array-subscript expansion nested inside the pattern of a ${var#pattern} / ${var%pattern} expansion, even though both the array element and the subscript variable are set. Without -u the same construct works. GNU bash 5.3.0 (and 5.2.21) accept it.
Found by the ecosweep2 lane driving bash-completion 2.18 core completions: _comp__reassemble_words() (bash_completion:1047 line=${line#*"${COMP_WORDS[i]}"}) dies on the first _comp_initialize -n ... call, killing every completion that excludes word-break chars (ssh-keygen, ssh, ...): rubash produces 0 replies where GNU produces 59 for ssh-keygen -.
Minimal reproducer (script file, run on both shells)
set -u
A=(x0 x1)
i=0
r=abc
r=${r#*"${A[i]}"}echo"[$r]"
GNU 5.3.0 (wsl /usr/local/bin/bash): prints [abc], rc=0
rubash (release build @116b12aa): line 5: r#*"${A[i]: unbound variable, rc=1
Variants probed (all crash on rubash, all pass on GNU):
subscript local to the function, array global; subscript global, array local; both local — scope does not matter
same construct without set -u — works on rubash (nounset is the trigger)
plain ${A[i]} outside a pattern context under set -u — works on rubash
GNU C source anchors (vendored third_party/bash, bash.git @b4608166)
subst.c:10320case '#': /* ${param#[#]pattern} */ — the pattern (value) reaches this case already expanded as an ordinary nested word; GNU does not apply any stricter boundness rule to expansions nested in a pattern.
subst.c:7663 parameter_brace_expand_word() (call sites 7861/7955) — the nested ${A[i]} resolves through the normal array-element lookup; with A[0] set there is no unbound condition (unbound reporting for subscripts guarded at subst.c:7500-7510, only when the var is invisible/absent).
Impact
bash-completion 2.18: every completion whose _comp_initialize passes -n :/-n := (word-break exclusion — ssh-keygen, ssh, curl-ish class) returns 0 candidates under rubash with an unbound variable stderr line, vs full candidate lists on GNU.
Family widened — ecosweep2 lane (liquidprompt own test suite). The same false-nounset fires on a much larger shape class than the original nested-array report. Isolated per-shape matrix (each case in its own subshell, set -u active, all vars set; GNU 5.3.0 passes every case with rc=0, rubash release @116b12aa rc=1 on the marked ones):
shape
example
rubash
# + bracket class
${line#[[:space:]]}
CRASHline#[[:space:]]: unbound variable
% + bracket class
${line%[[:space:]]}
CRASH
# + negated bracket (escaped /)
${p2#[!\/]}
CRASH
// + bracket class
${s2//[[:alpha:]]}
CRASH
// + plain bracket
${s2//[a,]}
CRASH
// + negated bracket
${p2//[!\/]}, ${s2//[!/]"}
CRASH
# + nested quoted array elem
${s2#*"${A[i]}"}
CRASH (original report)
# + literal
${line#x}
ok
%% + bracket class + *
${line%%[[:space:]]*}
ok
// + single char (+repl)
${s2//,}, ${s2//,/;}
ok
/ single + bracket + repl
${s2/[a]/X}
ok
Common symptom: the unbound diagnostic prints the whole expansion text as the variable name (q//[!\/]: unbound variable), i.e. the nounset check's name extraction is consuming the pattern. Without set -u every shape works, so the parse itself is fine — only the nounset path mis-extracts.
Blast radius (liquidprompt @afd7e83 own suite, ecosweep2): liquidprompt:1736 local slash_count="${display_path//[!\/]}" kills test_tools and test_bash_preexec; ${line#[[:space:]]}-class kills test_python_venv. Plus the bash-completion class from the original report.
Artifacts: target/issue-suites/results/ecosweep2/p-shapes.sh + rb-shapes.out/gnu-shapes.out (full matrix), p-negclass*.sh, liquidprompt suite outputs under lp/rb/.
Fixed by wt13/sweepfix (a6f9664a, master 203c2b5): nounset_braced_parameter_is_unbound fed the whole ${...} body to parse_array_subscript - bracket classes in patterns ([[:space:]]) were parsed as subscripts (line#[[:space:]] -> base line#, sub [:space:]) producing false unbound plus the whole body as the diagnostic name. New parameter_reference_len mirrors string_extract operator delimiting (subst.c:9807, :791, :812-818); operator tails checked per operator position (default/assign/alternate/all-elements bails); nounset_variable_bound now consults the typed store (array_p/assoc_p cell semantics). Family follow-ons fixed in the same pass: operator-tail diagnostic names, -/=/+ swallowed inside patterns, @-transform misses, and the text-heuristic is_array_storage misreading the literal () value. 8-shape issue matrix + 30-shape family matrix byte-identical on 5.3.0 AND 5.2.21; the old binary dies in set -u on ssh-keygen completion, the new one produces 59 replies byte-identical to GNU; three liquidprompt test files green again.
Activity
caomengxuan666 commentedon Sep 29, 2026
Family widened — ecosweep2 lane (liquidprompt own test suite). The same false-nounset fires on a much larger shape class than the original nested-array report. Isolated per-shape matrix (each case in its own subshell,
set -uactive, all vars set; GNU 5.3.0 passes every case with rc=0, rubash release @116b12aa rc=1 on the marked ones):#+ bracket class${line#[[:space:]]}line#[[:space:]]: unbound variable%+ bracket class${line%[[:space:]]}#+ negated bracket (escaped/)${p2#[!\/]}//+ bracket class${s2//[[:alpha:]]}//+ plain bracket${s2//[a,]}//+ negated bracket${p2//[!\/]},${s2//[!/]"}#+ nested quoted array elem${s2#*"${A[i]}"}#+ literal${line#x}%%+ bracket class +*${line%%[[:space:]]*}//+ single char (+repl)${s2//,},${s2//,/;}/single + bracket + repl${s2/[a]/X}Common symptom: the unbound diagnostic prints the whole expansion text as the variable name (
q//[!\/]: unbound variable), i.e. the nounset check's name extraction is consuming the pattern. Withoutset -uevery shape works, so the parse itself is fine — only the nounset path mis-extracts.Blast radius (liquidprompt @afd7e83 own suite, ecosweep2):
liquidprompt:1736 local slash_count="${display_path//[!\/]}"kills test_tools and test_bash_preexec;${line#[[:space:]]}-class kills test_python_venv. Plus the bash-completion class from the original report.Artifacts:
target/issue-suites/results/ecosweep2/p-shapes.sh+rb-shapes.out/gnu-shapes.out(full matrix),p-negclass*.sh, liquidprompt suite outputs underlp/rb/.docs(corpus): ecosweep2 lane section — second-pass fine-grained corpu…
fix(#311): nounset pattern-operator false unbound - pre-operator refe…
caomengxuan666 commentedon Sep 29, 2026
Fixed by wt13/sweepfix (a6f9664a, master 203c2b5): nounset_braced_parameter_is_unbound fed the whole ${...} body to parse_array_subscript - bracket classes in patterns ([[:space:]]) were parsed as subscripts (line#[[:space:]] -> base line#, sub [:space:]) producing false unbound plus the whole body as the diagnostic name. New parameter_reference_len mirrors string_extract operator delimiting (subst.c:9807, :791, :812-818); operator tails checked per operator position (default/assign/alternate/all-elements bails); nounset_variable_bound now consults the typed store (array_p/assoc_p cell semantics). Family follow-ons fixed in the same pass: operator-tail diagnostic names, -/=/+ swallowed inside patterns, @-transform misses, and the text-heuristic is_array_storage misreading the literal () value. 8-shape issue matrix + 30-shape family matrix byte-identical on 5.3.0 AND 5.2.21; the old binary dies in set -u on ssh-keygen completion, the new one produces 59 replies byte-identical to GNU; three liquidprompt test files green again.