Skip to content

Commit

Permalink
expand: avoid a panic when ParamExp.Repl.Orig is nil
Browse files Browse the repository at this point in the history
Pattern expected the word to never be nil, unlike other APIs
such as Literal or Document. Fix that to avoid other similar panics.

And when encountering a nil or empty ParamExp.Repl.Orig,
do not perform a search and replace, as it would result in every match.

Fixes #1076.
  • Loading branch information
mvdan committed Apr 28, 2024
1 parent 68768e5 commit 23633a4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions expand/expand.go
Expand Up @@ -214,6 +214,9 @@ const patMode = pattern.Filenames | pattern.Braces
// The config specifies shell expansion options; nil behaves the same as an
// empty config.
func Pattern(cfg *Config, word *syntax.Word) (string, error) {
if word == nil {
return "", nil
}
cfg = prepareConfig(cfg)
field, err := cfg.wordField(word.Parts, quoteNone)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions expand/param.go
Expand Up @@ -206,6 +206,9 @@ func (cfg *Config) paramExp(pe *syntax.ParamExp) (string, error) {
if err != nil {
return "", err
}
if orig == "" {
break // nothing to replace
}
with, err := Literal(cfg, pe.Repl.With)
if err != nil {
return "", err
Expand Down
1 change: 1 addition & 0 deletions interp/interp_test.go
Expand Up @@ -509,6 +509,7 @@ var runTests = []runTest{
{"a='abcx1y'; echo ${a//x[[:digit:]]y}", "abc\n"},
{`a=xyz; echo "${a/y/a b}"`, "xa bz\n"},
{"a='foo_interp_missing/bar_interp_missing'; echo ${a//o*a/}", "fr_interp_missing\n"},
{"a=foobar; echo ${a//a/} ${a///b} ${a///}", "foobr foobar foobar\n"},
{
"echo ${a:-b}; echo $a; a=; echo ${a:-b}; a=c; echo ${a:-b}",
"b\n\nb\nc\n",
Expand Down

0 comments on commit 23633a4

Please sign in to comment.