From 6e1ee078ccaa11479cd90c386198b84f94a18f72 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Thu, 18 Apr 2019 11:36:59 +0800 Subject: [PATCH 1/2] posix_realpath: make sure we don't loop infinitely On broken symlink loops this would never terminate. The most naive and easy solution is a max of loop steps. --- ghcup | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ghcup b/ghcup index 1d1a9e6..6ccb855 100755 --- a/ghcup +++ b/ghcup @@ -1131,12 +1131,20 @@ get_distro_alias() { # @STDOUT: realpath of the given file posix_realpath() { [ -z "$1" ] && die "Internal error: no argument given to posix_realpath" + current_loop=0 + max_loops=50 mysource=$1 while [ -h "${mysource}" ]; do + current_loop=$((current_loop+1)) mydir="$( cd -P "$( dirname "${mysource}" )" > /dev/null 2>&1 && pwd )" mysource="$(readlink "${mysource}")" [ "${mysource%${mysource#?}}"x != '/x' ] && mysource="${mydir}/${mysource}" + + if [ ${current_loop} -gt ${max_loops} ] ; then + (>&2 echo "${1}: Too many levels of symbolic links") + break + fi done mydir="$( cd -P "$( dirname "${mysource}" )" > /dev/null 2>&1 && pwd )" @@ -1149,7 +1157,7 @@ posix_realpath() { echo "${mydir%/}/$(basename "${mysource}")" fi - unset mysource mydir posix_realpath_error + unset current_loop max_loops mysource mydir } From 17aafe343961bbb73658a0f55c1ab1d1624e423e Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Thu, 18 Apr 2019 11:37:42 +0800 Subject: [PATCH 2/2] posix_realpath: if dir does not exist also append to current dir This matches realpath behavior. --- ghcup | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ghcup b/ghcup index 6ccb855..df1b215 100755 --- a/ghcup +++ b/ghcup @@ -1127,7 +1127,7 @@ get_distro_alias() { # and # https://stackoverflow.com/a/246128 # -# If the file does not exist, just prints the argument unchanged. +# If the file does not exist, just prints it appended to the current directory. # @STDOUT: realpath of the given file posix_realpath() { [ -z "$1" ] && die "Internal error: no argument given to posix_realpath" @@ -1151,8 +1151,6 @@ posix_realpath() { # TODO: better distinguish between "does not exist" and "permission denied" if [ -z "${mydir}" ] ; then (>&2 echo "${1}: Permission denied") - elif [ ! -e "$1" ] ; then - echo "${mysource}" else echo "${mydir%/}/$(basename "${mysource}")" fi