aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Afanasjew2016-06-29 16:20:47 +0200
committerMartin Afanasjew2016-06-29 16:20:47 +0200
commit3c5f007bfa1a469ef3e1a048ee163f3eaa73cb71 (patch)
treed1a273c0e92a447af40048c9f4bc79964a4de83d
parentda06e813c2b8925499484ff8be7772f6aa6ae9e3 (diff)
downloadbrew-3c5f007bfa1a469ef3e1a048ee163f3eaa73cb71.tar.bz2
bin/brew: fix corner cases in prefix computation
If `bin/brew` happens to be symlinked to `/brew`, `/bin/brew`, or some similar location or (worse yet) Homebrew is installed to `/`, then computation of the prefix and/or repository path could break down and result in an invalid or empty path. Closes Homebrew/homebrew-core#2430.
-rwxr-xr-xbin/brew7
1 files changed, 5 insertions, 2 deletions
diff --git a/bin/brew b/bin/brew
index b49b961e1..77611c153 100755
--- a/bin/brew
+++ b/bin/brew
@@ -5,10 +5,13 @@ quiet_cd() {
cd "$@" >/dev/null
}
-BREW_FILE_DIRECTORY="$(quiet_cd "${0%/*}" && pwd -P)"
-HOMEBREW_BREW_FILE="$BREW_FILE_DIRECTORY/${0##*/}"
+BREW_FILE_DIRECTORY="$(quiet_cd "${0%/*}/" && pwd -P)"
+HOMEBREW_BREW_FILE="${BREW_FILE_DIRECTORY%/}/${0##*/}"
HOMEBREW_PREFIX="${HOMEBREW_BREW_FILE%/*/*}"
+[[ -n "$HOMEBREW_PREFIX" && "$HOMEBREW_PREFIX" != "$HOMEBREW_BREW_FILE" ]] \
+ || HOMEBREW_PREFIX="/"
+
HOMEBREW_REPOSITORY="$HOMEBREW_PREFIX"
if [[ -L "$HOMEBREW_BREW_FILE" ]]