Skip to content

fix(complete): compgen -G returns literal-path matches and globs under set -f (Refs #4272) - #4278

Merged
caomengxuan666 merged 1 commit into
masterfrom
captain/4272-compgen-G-literal
Nov 29, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
captain/4272-compgen-G-literal

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

UNVERIFIED-BY-CAPTAIN

Refs #4272 (not Fixes — captain closes after verification).

What

compgen -G diverged from GNU on two faces (ShiomiHC's report, captain-confirmed against the bash 5.3.20 oracle, triage comment on #4272):

  1. A pattern with no unquoted glob characters (or only backslash-escaped ones) never produced a result, even when the path exists. GNU returns the path, dequoted, when it exists.
  2. Under set -f, compgen -G returned nothing at all, even for real patterns. GNU still matches.

Root cause

pcomplete.c gen_globpat_matches hands the pattern straight to glob_filename; the subst.c word-expansion admission gates never apply. rubash routed compgen through pathname_expand_word, whose gates are exactly those word-expansion checks: the byte whitelist + unquoted_glob_pattern_p (a pattern-free word is provably not a pattern for expansion) and the noglob early return (a glob_expand_word_list check, not a glob.c one).

Fix

New compgen_globpat_expand_word in src/executor/glob.rs: the same machinery as pathname_expand_word (shared pathname_expand_word_gated body) with the word-expansion gates skipped — glob.c's own per-component glob_pattern_p admission (stat fast path in glob_vector, only_filename branch in glob_filename) decides instead. Both compgen call sites in src/builtins/complete.rs (the builtin and the compspec completion attempt, which share gen_globpat_matches in GNU) use it.

Ordinary expansion is untouched, and the pins guard the separation: set -f still suppresses echo fi*; nullglob/failglob keep their word-list behavior and neither leaks into compgen (a -G no-match stays a quiet rc 1 under both).

Consequences that fall out of the machinery, all oracle-verified byte-for-byte against bash 5.3.20: literal file/dir returned dequoted (fix, fix/a.dat, fix/a\.dat → fix/a.dat), trailing-slash words returned verbatim with no existence check (fix/, nope/, fix/a.dat/), ./fix, fix/., fix//a.dat preserved, nonexistent literal paths and paths under nonexistent directories return nothing (rc 1), empty pattern yields one empty match (rc 0), invalid bracket text is literal (returned only when the file exists).

Verification (lane, devin-mac)

  • Fail-first: reporter's script + boundary cases on master 75a7947b reproduced both faces (every literal case rc 1, every set -f case rc 1); after the fix the full 21-case table matches the oracle verbatim, including abs ok.
  • Pins: tests/issues_4200_4299/issue4272_compgen_globpat_literal_and_noglob.rs, 5 tests — bucket runs 38/38.
  • Census differential (local macOS, same-environment master baseline run back to back): fixed 632 FAIL / master 629 FAIL of ~12.3k; symmetric difference is 6 coproc-family names only-in-fixed vs 3 coproc/procsub names only-in-master — the documented coproc rotation, zero names in the glob/compgen/completion blast radius; 0 SIGABRT on both sides; TIMEOUT sets identical (6). The CI (ubuntu) census + Windows goldens remain captain-side checks; the goldens are #[cfg(windows)] and cannot run on the mac lane.

Net production lines: src +48/−9 (glob.rs +46/−7 incl. doc comments, complete.rs +2/−2 for the two call sites); tests +182.

…r set -f (Refs #4272)

Refs #4272 (not Fixes).

gen_globpat_matches (pcomplete.c) hands the -G pattern straight to
glob_filename; the subst.c word-expansion admission gates never
apply. rubash routed compgen through pathname_expand_word, whose
gates dropped two faces: a pattern without unquoted metacharacters
never produced a result (glob_vector's stat fast path returns the
dequoted literal when the file exists), and under `set -f` every
-G pattern returned nothing (noglob is a glob_expand_word_list
check, not a glob.c one).

Add compgen_globpat_expand_word in glob.rs: the same machinery as
pathname_expand_word with the word-expansion gates (empty word,
byte whitelist, noglob, whole-word unquoted_glob_pattern_p)
skipped; glob.c's per-component glob_pattern_p admission decides
instead. Both compgen call sites in complete.rs (the builtin and
the compspec completion attempt) use it. Ordinary expansion is
untouched: `set -f` still suppresses echo fi*, nullglob/failglob
keep their word-list behavior, and neither leaks into compgen.

Pins: tests/issues_4200_4299/issue4272_compgen_globpat_literal_
and_noglob.rs (5 tests: literal face incl. escapes/trailing
slash/absolute/nonexistent, noglob face + expansion guard, empty
pattern, invalid bracket + prefix filter, nullglob/failglob
neutrality). Oracle-verified against bash 5.3.20 on every case.
@caomengxuan666
caomengxuan666 merged commit 60ed70e into master Nov 29, 2026
11 of 13 checks passed
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