makeWrapper: Don't glob in prefix/suffix

Disable file globbing in --prefix/--suffix, since bash will otherwise
try to find filenames matching the the value to be prefixed/suffixed
if it contains characters considered wildcards, such as `?` and
`*`. We want the value as is, except we also want to split it on on
the separator; hence we can't quote it.
This commit is contained in:
talyz 2022-02-12 02:11:33 +01:00
parent 2ea430e624
commit 183147a037
No known key found for this signature in database
GPG Key ID: 2DED2151F4671A2B
2 changed files with 20 additions and 1 deletions

View File

@ -51,7 +51,19 @@ makeWrapper() {
local varName="$2" # name of list variable to add to
local separator="$3" # character used to separate elements of list
local value="$4" # one value, or multiple values separated by `separator`, to add to list
if test -n "$value"; then
# Disable file globbing, since bash will otherwise try to find
# filenames matching the the value to be prefixed/suffixed if
# it contains characters considered wildcards, such as `?` and
# `*`. We want the value as is, except we also want to split
# it on on the separator; hence we can't quote it.
local reenableGlob=0
if [[ ! -o noglob ]]; then
reenableGlob=1
fi
set -o noglob
if [[ -n "$value" ]]; then
local old_ifs=$IFS
IFS=$separator
@ -86,6 +98,10 @@ makeWrapper() {
done
IFS=$old_ifs
fi
if (( reenableGlob )); then
set +o noglob
fi
}
mkdir -p "$(dirname "$wrapper")"

View File

@ -64,6 +64,7 @@ runCommand "make-wrapper-test"
(mkWrapperBinary { name = "test-run-and-set"; args = [ "--run" "export VAR=foo" "--set" "VAR" "bar" ]; })
(mkWrapperBinary { name = "test-args"; args = [ "--add-flags" "abc" ]; wrapped = wrappedBinaryArgs; })
(mkWrapperBinary { name = "test-prefix"; args = [ "--prefix" "VAR" ":" "abc" ]; })
(mkWrapperBinary { name = "test-prefix-noglob"; args = [ "--prefix" "VAR" ":" "./*" ]; })
(mkWrapperBinary { name = "test-suffix"; args = [ "--suffix" "VAR" ":" "abc" ]; })
(mkWrapperBinary { name = "test-prefix-and-suffix"; args = [ "--prefix" "VAR" ":" "foo" "--suffix" "VAR" ":" "bar" ]; })
(mkWrapperBinary { name = "test-prefix-multi"; args = [ "--prefix" "VAR" ":" "abc:foo:foo" ]; })
@ -112,6 +113,8 @@ runCommand "make-wrapper-test"
# Only append the value once when given multiple times in a parameter
# to makeWrapper
+ mkTest "test-prefix" "VAR=abc"
# --prefix doesn't expand globs
+ mkTest "VAR=f?oo test-prefix-noglob" "VAR=./*:f?oo"
# --suffix works