Skip to content

Commit 08dd2cd

Browse files
committed
fix(nounset): fix nounset errors of -W '${arr[@]}'
In Bash 4.3, "${arr[@]}" for an empty array "arr=()" fails under the setting "set -o nounset". This causes error messages with "compgen -W '${arr[@]}'". In this commit, we check that the array is non-empty before trying to call "compgen -W".
1 parent 76b7925 commit 08dd2cd

File tree

22 files changed

+101
-62
lines changed

22 files changed

+101
-62
lines changed

bash_completion

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ else
12331233
# @param $1 if -s, don't try to avoid truncated command names
12341234
_pnames()
12351235
{
1236-
local -a procs
1236+
local -a procs=()
12371237
if [[ ${1-} == -s ]]; then
12381238
procs=($(command ps ax -o comm | command sed -e 1d))
12391239
else
@@ -1277,7 +1277,8 @@ else
12771277
done
12781278
fi
12791279
fi
1280-
COMPREPLY=($(compgen -X "<defunct>" -W '${procs[@]}' -- "$cur"))
1280+
((${#procs[@]})) &&
1281+
COMPREPLY=($(compgen -X "<defunct>" -W '"${procs[@]}"' -- "$cur"))
12811282
}
12821283
fi
12831284

@@ -1324,7 +1325,7 @@ _xinetd_services()
13241325
local -a svcs=($xinetddir/!($_backup_glob))
13251326
$reset
13261327
((!${#svcs[@]})) ||
1327-
COMPREPLY+=($(compgen -W '${svcs[@]#$xinetddir/}' -- "${cur-}"))
1328+
COMPREPLY+=($(compgen -W '"${svcs[@]#$xinetddir/}"' -- "${cur-}"))
13281329
fi
13291330
}
13301331

@@ -1882,7 +1883,7 @@ _known_hosts_real()
18821883
local -a hosts=($(command sed -ne 's/^[[:blank:]]*[Hh][Oo][Ss][Tt][[:blank:]=]\{1,\}\(.*\)$/\1/p' "${config[@]}"))
18831884
if ((${#hosts[@]} != 0)); then
18841885
COMPREPLY+=($(compgen -P "$prefix" \
1885-
-S "$suffix" -W '${hosts[@]%%[*?%]*}' -X '\!*' -- "$cur"))
1886+
-S "$suffix" -W '"${hosts[@]%%[*?%]*}"' -X '\!*' -- "$cur"))
18861887
fi
18871888
fi
18881889

completions/_adb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ _adb()
4242
tmp+=($($1 help 2>&1 | awk '$1 == "adb" { print $2 }'))
4343
tmp+=(devices connect disconnect sideload)
4444
fi
45-
COMPREPLY=($(compgen -W '${tmp[@]}' -- "$cur"))
45+
((${#tmp[@]})) &&
46+
COMPREPLY=($(compgen -W '"${tmp[@]}"' -- "$cur"))
4647
return
4748
fi
4849

completions/chronyc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ _chronyc_command_args()
88
case $args in
99
\<address\>) _known_hosts_real -- "$cur" ;;
1010
\<*) ;;
11-
*) COMPREPLY+=($(compgen -W '${args[@]}' -- "$cur")) ;;
11+
*) ((${#args[@]})) &&
12+
COMPREPLY+=($(compgen -W '"${args[@]}"' -- "$cur")) ;;
1213
esac
1314
}
1415

completions/curl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,13 @@ _curl()
106106
$("$1" --help non-existent-category 2>&1 |
107107
awk '/^[[:space:]]/ {print $1}')
108108
)
109-
for x in "${categories[@]}"; do
110-
# Looks like an option? Likely no --help category support
111-
[[ $x != -* ]] || return
112-
done
113-
COMPREPLY=($(compgen -W '${categories[@]}' -- "$cur"))
109+
if ((${#categories[@]})); then
110+
for x in "${categories[@]}"; do
111+
# Looks like an option? Likely no --help category support
112+
[[ $x != -* ]] || return
113+
done
114+
COMPREPLY=($(compgen -W '${categories[@]}' -- "$cur"))
115+
fi
114116
return
115117
;;
116118
--krb)

completions/cvs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ _cvs()
158158
for i in "${!files[@]}"; do
159159
if [[ ${files[i]} == ?(*/)CVS ]]; then
160160
unset 'files[i]'
161-
else
161+
elif ((${#entries[@]})); then
162162
for f in "${entries[@]}"; do
163163
if [[ ${files[i]} == "$f" && ! -d $f ]]; then
164164
unset 'files[i]'
@@ -167,8 +167,9 @@ _cvs()
167167
done
168168
fi
169169
done
170-
COMPREPLY=($(compgen -X "$_backup_glob" -W '${files[@]}' \
171-
-- "$cur"))
170+
((${#files[@]})) &&
171+
COMPREPLY=($(compgen -X "$_backup_glob" -W '"${files[@]}"' \
172+
-- "$cur"))
172173
else
173174
_cvs_command_options "$1" $mode
174175
fi
@@ -193,7 +194,8 @@ _cvs()
193194
_cvs_command_options "$1" $mode
194195
else
195196
_cvs_entries
196-
COMPREPLY=($(compgen -W '${entries[@]}' -- "$cur"))
197+
((${#entries[@]})) &&
198+
COMPREPLY=($(compgen -W '"${entries[@]}"' -- "$cur"))
197199
fi
198200
;;
199201
annotate)
@@ -203,7 +205,8 @@ _cvs()
203205
_cvs_command_options "$1" $mode
204206
else
205207
_cvs_entries
206-
COMPREPLY=($(compgen -W '${entries[@]}' -- "$cur"))
208+
((${#entries[@]})) &&
209+
COMPREPLY=($(compgen -W '"${entries[@]}"' -- "$cur"))
207210
fi
208211
;;
209212
checkout)
@@ -256,11 +259,14 @@ _cvs()
256259
command sed -ne 's/^Files [^ ]* and \([^ ]*\) differ$/\1/p'))
257260
newremoved=($(cvs -q diff --brief 2>&1 |
258261
command sed -ne 's/^cvs diff: \([^ ]*\) .*, no comparison available$/\1/p'))
259-
COMPREPLY=($(compgen -W '${changed[@]:-} \
260-
${newremoved[@]:-}' -- "$cur"))
262+
((${#changed[@]})) && COMPREPLY+=("${changed[@]}")
263+
((${#newremoved[@]})) && COMPREPLY+=("${newremoved[@]}")
264+
((${#COMPREPLY[@]})) &&
265+
COMPREPLY=($(compgen -W '"${COMPREPLY[@]}"' -- "$cur"))
261266
else
262267
_cvs_entries
263-
COMPREPLY=($(compgen -W '${entries[@]}' -- "$cur"))
268+
((${#entries[@]})) &&
269+
COMPREPLY=($(compgen -W '"${entries[@]}"' -- "$cur"))
264270
fi
265271
else
266272
_cvs_command_options "$1" $mode
@@ -275,15 +281,17 @@ _cvs()
275281
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
276282
else
277283
_cvs_entries
278-
COMPREPLY=($(compgen -W '${entries[@]:-}' -- "$cur"))
284+
((${#entries[@]})) &&
285+
COMPREPLY=($(compgen -W '"${entries[@]}"' -- "$cur"))
279286
fi
280287
;;
281288
editors | watchers)
282289
if [[ $cur == -* ]]; then
283290
_cvs_command_options "$1" $mode
284291
else
285292
_cvs_entries
286-
COMPREPLY=($(compgen -W '${entries[@]}' -- "$cur"))
293+
((${#entries[@]})) &&
294+
COMPREPLY=($(compgen -W '"${entries[@]}"' -- "$cur"))
287295
fi
288296
;;
289297
export)
@@ -350,7 +358,8 @@ _cvs()
350358
[[ -r ${entries[i]} ]] && unset 'entries[i]'
351359
done
352360
fi
353-
COMPREPLY=($(compgen -W '${entries[@]:-}' -- "$cur"))
361+
((${#entries[@]})) &&
362+
COMPREPLY=($(compgen -W '"${entries[@]}"' -- "$cur"))
354363
else
355364
_cvs_command_options "$1" $mode
356365
fi
@@ -371,7 +380,8 @@ _cvs()
371380
_cvs_command_options "$1" $mode
372381
else
373382
_cvs_entries
374-
COMPREPLY=($(compgen -W '${entries[@]}' -- "$cur"))
383+
((${#entries[@]})) &&
384+
COMPREPLY=($(compgen -W '"${entries[@]}"' -- "$cur"))
375385
fi
376386
;;
377387
"")

completions/dot

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ _dot()
1414
-T*)
1515
local langs=($("$1" -TNON_EXISTENT 2>&1 |
1616
command sed -ne 's/.*one of://p'))
17-
COMPREPLY=($(compgen -P -T -W '${langs[@]}' -- "${cur#-T}"))
17+
((${#langs[@]})) &&
18+
COMPREPLY=($(compgen -P -T -W '"${langs[@]}"' -- "${cur#-T}"))
1819
return
1920
;;
2021
-K*)
2122
local layouts=($("$1" -KNON_EXISTENT 2>&1 |
2223
command sed -ne 's/.*one of://p'))
23-
COMPREPLY=($(compgen -P -K -W '${layouts[@]}' -- "${cur#-K}"))
24+
((${#layouts[@]})) &&
25+
COMPREPLY=($(compgen -P -K -W '"${layouts[@]}"' -- "${cur#-K}"))
2426
return
2527
;;
2628
-o*)

completions/dpkg

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ _dpkg_reconfigure()
133133
case $prev in
134134
--frontend | -!(-*)f)
135135
opt=(/usr/share/perl5/Debconf/FrontEnd/*)
136-
opt=(${opt[@]##*/})
137-
opt=(${opt[@]%.pm})
138-
COMPREPLY=($(compgen -W '${opt[@]}' -- "$cur"))
136+
if ((${#opt[@]})); then
137+
opt=(${opt[@]##*/})
138+
opt=(${opt[@]%.pm})
139+
COMPREPLY=($(compgen -W '${opt[@]}' -- "$cur"))
140+
fi
139141
return
140142
;;
141143
--priority | -!(-*)p)

completions/gdb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ _gdb()
3030
command sed -e 's/:\{2,\}/:/g' -e 's/^://' -e 's/:$//' <<<"$PATH"
3131
))
3232
IFS=$'\n'
33-
COMPREPLY=($(compgen -d -W '$(find "${path_array[@]}" . \
33+
COMPREPLY=($(compgen -d -W '$(find ${path_array[@]+"${path_array[@]}"} . \
3434
-mindepth 1 -maxdepth 1 -not -type d -executable \
3535
-printf "%f\\n" 2>/dev/null)' -- "$cur"))
3636
fi

completions/hunspell

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ _hunspell()
1414
shopt -s nullglob
1515
local -a dicts=(/usr/share/hunspell/*.dic
1616
/usr/local/share/hunspell/*.dic)
17-
dicts=("${dicts[@]##*/}")
18-
dicts=("${dicts[@]%.dic}")
1917
$reset
20-
IFS=$'\n'
21-
COMPREPLY=($(compgen -W '${dicts[@]}' -- "$cur"))
18+
if ((${#dicts[@]})); then
19+
dicts=("${dicts[@]##*/}")
20+
dicts=("${dicts[@]%.dic}")
21+
IFS=$'\n'
22+
COMPREPLY=($(compgen -W '${dicts[@]}' -- "$cur"))
23+
fi
2224
return
2325
;;
2426
-i)

completions/invoke-rc.d

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ _invoke_rc_d()
2323
command sed -ne "/$(command sed 's/ /\\|/g' <<<"${options[*]}")/p" |
2424
sort | uniq -u
2525
))
26-
COMPREPLY=($(compgen -W '${valid_options[@]} ${services[@]}' -- "$cur"))
26+
((${#valid_options[@]})) && COMPREPLY+=("${valid_options[@]}")
27+
((${#services[@]})) && COMPREPLY+=("${services[@]}")
28+
((${#COMPREPLY[@]})) &&
29+
COMPREPLY=($(compgen -W '"${COMPREPLY[@]}"' -- "$cur"))
2730
elif [[ -x $sysvdir/$prev ]]; then
2831
COMPREPLY=($(compgen -W '`command sed -e "y/|/ /" \
2932
-ne "s/^.*Usage:[ ]*[^ ]*[ ]*{*\([^}\"]*\).*$/\1/p" \

0 commit comments

Comments
 (0)