Skip to content

run function is susceptible to globbing #281

@dimo414

Description

@dimo414

This line is not a safe way to split arbitrary data into lines (as the suppressed SC2206 notes), as it relies on Bash's wordsplitting logic which also expands globs.

MCVE:

$ bats --version
Bats 1.1.0

$ ls
bats-bug.bats  some-file

$ cat bats-bug.bats
#!/usr/bin/env bats

@test "bats bug" {
  run printf 'foo\n*\nbar\n'
  printf 'Output:\n%s\n\n' "$output"
  echo "Lines:"
  printf '  %s\n' "${lines[@]}"
  [[ "${lines[1]}" == "*" ]]
}

$ bats bats-bug.bats
 ✗ bats bug
   (in test file bats-bug.bats, line 7)
     `printf '  %s\n' "${lines[@]}"' failed
   Output:
   foo
   *
   bar

   Lines:
     foo
     bats-bug.bats
     some-file
     bar

1 test, 1 failure

I can send a PR to use while read instead, assuming there's no reason this behavior is intended (e.g. backwards compatibility).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: Bash CodeEverything regarding the bash codePriority: CriticalBroken behavior in nearly all environments, e.g. wrong test results, internal bats errorStatus: ConfirmedThe reproducer worked as describedType: Bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions