From e1690f61ffc88d0fd68bf4e705d802e8fd100ee0 Mon Sep 17 00:00:00 2001 From: sterlingjensen <5555776+sterlingjensen@users.noreply.github.com> Date: Sat, 20 Feb 2021 07:58:52 -0600 Subject: [PATCH] package: Fix unspecified behavior after "unset PWD" (#176) POSIX warns about "unset PWD" leading to unspecified behavior from the pwd util, which we then use. bin/package, src/cmd/INIT/package.sh: - Determine if the shell has $PWD with a feature test. Only unset PWD if it does not (the old Bourne shell). - Clean up 'case' flow. Co-authored-by: Martijn Dekker --- bin/package | 59 +++++++++++++++++++---------------------- src/cmd/INIT/package.sh | 59 +++++++++++++++++++---------------------- 2 files changed, 54 insertions(+), 64 deletions(-) diff --git a/bin/package b/bin/package index c43a382fe..9b0ded895 100755 --- a/bin/package +++ b/bin/package @@ -29,49 +29,44 @@ case $PATH in Bad*) echo "Cannot be run by zsh in native mode; use a sh symlink to zsh" >&2 exit 1 ;; esac +unset path -unset CDPATH PWD +# Sanitize 'cd' +unset CDPATH +case `cd / && v=$PWD && cd /dev && v=$v,$PWD && echo "$v"` in +/,/dev) ;; +*) # old Bourne shell does not have $PWD; avoid inheriting it from + # the environment, which would kill our ${PWD:-`pwd`} fallback + unset PWD ;; +esac + +# Make the package root the current working directory case $0 in -*) echo "dodgy \$0: $0" >&2 exit 1 ;; */package) - mydir=`echo "$0" | sed 's,/package$,,'` - cd "$mydir" || exit - case ${PWD:-`pwd`} in - */bin) ;; - *) echo "this script must live in bin/" >&2 - exit 1 ;; - esac - case $mydir in - */arch/*/*/bin) - cd .. ;; - */arch/*/bin) - cd ../../.. ;; - *) cd .. ;; - esac || exit - unset mydir ;; + pwd=$0 ;; package) - me=`command -v package` || me=`which package` || exit - mydir=`echo "$me" | sed 's,/package$,,'` - cd "$mydir" || exit - case ${PWD:-`pwd`} in - */bin) ;; - *) echo "this script must live in bin/" >&2 - exit 1 ;; - esac - case $mydir in - */arch/*/*/bin) - cd .. ;; - */arch/*/bin) - cd ../../.. ;; - *) cd .. ;; - esac || exit - unset me mydir ;; + pwd=`command -v package || which package` || exit ;; *) echo "this script must be named 'package'" >&2 exit 1 ;; esac +pwd=`dirname "$pwd"` +cd "$pwd" || exit +case ${PWD:-`pwd`} in +*/arch/*/*/bin) + cd .. ;; +*/arch/*/bin) + cd ../../.. ;; +*/bin) + cd .. ;; +*) + echo "this script must live in bin/" >&2 + exit 1 ;; +esac || exit +unset pwd # shell checks checksh() diff --git a/src/cmd/INIT/package.sh b/src/cmd/INIT/package.sh index c43a382fe..9b0ded895 100644 --- a/src/cmd/INIT/package.sh +++ b/src/cmd/INIT/package.sh @@ -29,49 +29,44 @@ case $PATH in Bad*) echo "Cannot be run by zsh in native mode; use a sh symlink to zsh" >&2 exit 1 ;; esac +unset path -unset CDPATH PWD +# Sanitize 'cd' +unset CDPATH +case `cd / && v=$PWD && cd /dev && v=$v,$PWD && echo "$v"` in +/,/dev) ;; +*) # old Bourne shell does not have $PWD; avoid inheriting it from + # the environment, which would kill our ${PWD:-`pwd`} fallback + unset PWD ;; +esac + +# Make the package root the current working directory case $0 in -*) echo "dodgy \$0: $0" >&2 exit 1 ;; */package) - mydir=`echo "$0" | sed 's,/package$,,'` - cd "$mydir" || exit - case ${PWD:-`pwd`} in - */bin) ;; - *) echo "this script must live in bin/" >&2 - exit 1 ;; - esac - case $mydir in - */arch/*/*/bin) - cd .. ;; - */arch/*/bin) - cd ../../.. ;; - *) cd .. ;; - esac || exit - unset mydir ;; + pwd=$0 ;; package) - me=`command -v package` || me=`which package` || exit - mydir=`echo "$me" | sed 's,/package$,,'` - cd "$mydir" || exit - case ${PWD:-`pwd`} in - */bin) ;; - *) echo "this script must live in bin/" >&2 - exit 1 ;; - esac - case $mydir in - */arch/*/*/bin) - cd .. ;; - */arch/*/bin) - cd ../../.. ;; - *) cd .. ;; - esac || exit - unset me mydir ;; + pwd=`command -v package || which package` || exit ;; *) echo "this script must be named 'package'" >&2 exit 1 ;; esac +pwd=`dirname "$pwd"` +cd "$pwd" || exit +case ${PWD:-`pwd`} in +*/arch/*/*/bin) + cd .. ;; +*/arch/*/bin) + cd ../../.. ;; +*/bin) + cd .. ;; +*) + echo "this script must live in bin/" >&2 + exit 1 ;; +esac || exit +unset pwd # shell checks checksh()