errors with set -o nounset

Bug #663111 reported by Ian! D. Allen
38
This bug affects 6 people
Affects Status Importance Assigned to Milestone
bash-completion (Ubuntu)
Confirmed
Undecided
Unassigned

Bug Description

Binary package hint: bash-completion

The main /etc/bash_completion script and many of the helper scripts
under /etc/bash_completion.d/ fail for shells running with one or both
of "set -o nounset" (fail if variables are used without being defined)
and/or "shopt -s failglob" (fail if a GLOB pattern fails to match).
I run my interactive shells with both these options set.

For example:

bash$ set -o nounset
bash$ source /etc/bash_completion.ORIG
bash: BASH_COMPLETION_DEBUG: unbound variable
bash: BASH_COMPLETION: unbound variable
bash: BASH_COMPLETION_DIR: unbound variable
bash: BASH_COMPLETION_COMPAT_DIR: unbound variable
bash: BASH_COMPLETION: unbound variable
bash: list[@]: unbound variable
bash: BASH_COMPLETION_COMPAT_DIR: unbound variable
bash: BASH_COMPLETION_DIR: unbound variable
bash: BASH_COMPLETION: unbound variable

Also, a few of the "eval" expressions in the scripts use backslashes
but fail to fully protect square bracket characters on arrays, which
are then taken as failed GLOB expressions.

The attached patch fixes at least some of the problems with the main
script so that it runs without error for shells using "set -o nounset"
and "shopt -s failglob".

The helper scripts for findutils and mutt in /etc/bash_completion.d/
also have errors when used in shells using "set -o nounset" and "shopt
-s failglob", but I'm not so confident that I understand them to fix
them correctly. Here are some patches that remove some of the errors
in these scripts but don't seem to fully work properly:

--- .findutils.ORIG 2010-08-06 19:39:13.000000000 -0400
+++ findutils 2010-10-18 11:52:07.982166655 -0400
@@ -5,7 +5,7 @@
 have find &&
 _find()
 {
- local cur prev i exprfound onlyonce
+ local cur prev i exprfound= onlyonce

     COMPREPLY=()
     _get_comp_words_by_ref cur prev

--- .mutt.ORIG 2010-08-06 19:39:13.000000000 -0400
+++ mutt 2010-10-12 11:23:48.717163963 -0400
@@ -22,6 +22,7 @@
 _muttrc()
 {
     # Search COMP_WORDS for '-F muttrc' or '-Fmuttrc' argument
+ local muttrc=
     set -- "${COMP_WORDS[@]}"
     while [ $# -gt 0 ]; do
         if [ "${1:0:2}" = -F ]; then
@@ -55,13 +56,14 @@

     sofar=" $1 "
     shift
- while [[ "$1" ]]; do
- newconffiles=( $(sed -n 's|^source[[:space:]]\{1,\}\([^[:space:]]\{1,\}\).*$|\1|p' $(eval echo $1) ) )
- for file in "${newconffiles[@]}"; do
+ while [[ $# -gt 0 && "$1" ]]; do
+ newconffiles=( $(sed -n 's|^source[[:space:]]\{1,\}\([^[:space:]]\{1,\}\).*$|\1|p' "$1" ) )
+ for file in "${newconffiles[@]-}"; do
             [[ ! "$file" ]] || [[ "${sofar/ ${file} / }" != "$sofar" ]] &&
                 continue
             sofar="$sofar $file"
- sofar=" $(eval _muttconffiles \"$sofar\" $file) "
+ sofar=$(_muttconffiles "$sofar" "$file")
         done
         shift
     done
@@ -80,8 +82,8 @@

     conffiles=( $(eval _muttconffiles $muttrc $muttrc) )
     aliases=( $( sed -n 's|^alias[[:space:]]\{1,\}\([^[:space:]]\{1,\}\).*$|\1|p' \
- $(eval echo "${conffiles[@]}") ) )
- COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W "${aliases[*]}" -- "$cur" ) )
+ $(eval echo "${conffiles[@]:-/dev/null}") ) )
+ COMPREPLY=( "${COMPREPLY[@]-}" $( compgen -W "${aliases[*]-}" -- "$cur" ) )

     return 0
 }
@@ -101,7 +103,7 @@
             sed -n '2,$s|^\([^[:space:]]\{1,\}\).*|\1|p' ) )
     fi

- COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W "${queryresults[*]}" \
+ COMPREPLY=( "${COMPREPLY[@]-}" $( compgen -W "${queryresults[*]-}" \
         -- "$cur" ) )

     return 0

ProblemType: Bug
DistroRelease: Ubuntu 10.10
Package: bash-completion 1:1.2-2ubuntu1
ProcVersionSignature: Ubuntu 2.6.35-22.34-generic 2.6.35.4
Uname: Linux 2.6.35-22-generic x86_64
Architecture: amd64
Date: Tue Oct 19 02:08:30 2010
EcryptfsInUse: Yes
InstallationMedia: Ubuntu 10.10 "Maverick Meerkat" - Release amd64 (20101007)
PackageArchitecture: all
ProcEnviron:
 LC_CTYPE=en_CA.utf8
 LC_COLLATE=C
 PATH=(custom, user)
 LANG=C
 SHELL=/bin/bash
SourcePackage: bash-completion

Revision history for this message
Ian! D. Allen (idallen) wrote :
Revision history for this message
Ian! D. Allen (idallen) wrote :

Another "undefined variable" error, but I'm not sure if the fix is correct:

--- .desktop-file-validate.ORIG 2010-09-08 06:14:56.000000000 -0400
+++ desktop-file-validate 2010-10-11 16:18:22.215080283 -0400
@@ -9,4 +9,4 @@
  cur=${COMP_WORDS[COMP_CWORD]}
  _filedir '@(desktop)'
 }
-[ "${have:-}" ] && complete -F _desktop_file_validate $filenames desktop-file-validate
+[ "${have:-}" ] && complete -F _desktop_file_validate -o filenames desktop-file-validate

Revision history for this message
Ian! D. Allen (idallen) wrote :

And the /etc/bash_completion.d/mutt script fails badly if you have a line using pipes such as this in your .muttrc:

source ~/sh/muttaliases.sh|

bash$ mutt <TAB>sed: can't read ~/sh/muttaliases.sh|: No such file or directory
bash: eval: line -150: syntax error: unexpected end of file
^C -- ^C needed because sed is reading your keyboard and the shell hangs here
Exit 1

tags: added: patch
Revision history for this message
Ian! D. Allen (idallen) wrote :
Changed in bash-completion (Ubuntu):
status: New → Confirmed
Revision history for this message
Ralph Corderoy (ralph-inputplus) wrote :

Trivial to reproduce. I've just upgraded from 8.04 -> 10.04 -> 10.10 and it seems bash_completion has been re-enabled for me somewhere along the line. Because I have failglob set <Tab> filename completion is broken.

    $ shopt -u failglob
    $ ls /nonexistant^C
    $ shopt -s failglob
    $ ls /nonexistant-bash: no match: "${COMP_WORDS[@]}"
    $

Both times I typed Tab after "nonexistant". The first time there's no matches and I press ^C to give up. The second time bash immediately gives that error and aborts the line I was part-way through entering.

Revision history for this message
daisy stanton (stanton-r) wrote :

Does anyone have a workaround for this?

Revision history for this message
Ralph Corderoy (ralph-inputplus) wrote :

Hi Daisy, No workaround that I know of.
https://bugs.launchpad.net/ubuntu/+source/bash-completion/+bug/194419/comments/29
suggests wrapping the entry and exit of the broken functions with a
save-and-restore of failglob.
https://wiki.debian.org/Teams/BashCompletion/Proposals/Roadmap says 3.0
will fix it. That the head `2.x' exists in git at
https://alioth.debian.org/scm/browser.php?group_id=100114 suggests
head `master' might be 3.0 in progress so using that might fare better.

Revision history for this message
Peter Cordes (peter-cordes) wrote :

on Trusty, nothing obvious goes wrong with shopt -s failglob.

set -o nounset breaks everything, though.

summary: - bash_completion script errors: undefined variables, failed GLOB
+ errors with set -o nounset
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.