Merge pull request #3515 from fudianchn/fix/version-file-relative-loop-3513

Stop infinite loop in pyenv version-file for relative paths
This commit is contained in:
native-api 2026-08-10 13:08:04 +03:00 committed by GitHub
commit e234812f46
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 4 deletions

View file

@ -8,12 +8,28 @@ target_dir="$1"
find_local_version_file() {
local root="$1"
while ! [[ "$root" =~ ^//[^/]*$ ]]; do
if [ -f "${root}/.python-version" ]; then
echo "${root}/.python-version"
# Nonexistent paths are UB as of this writing.
# Relative ones cause a failure but absolute ones don't
[[ $root != /* ]] && root=$(CDPATH= cd -- "$root" && pwd)
# Original Rbenv code supports UNC notaion for Cygwin/MinGW
# (https://github.com/rbenv/rbenv/pull/529)
# POSIX.1-2024 still allows to treat //<name> in implementation-specific manner
# (https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap04.html#tag_04_16)
# even though few UNIX variants do that
local unc; [[ $root =~ ^//[^/] && ! / -ef // ]] && unc=1
root="${root%/}"
# when testing root, $root is ""
while true; do
# don't test //.python-version if // is special
# as it's pointless and possibly very slow
# if it e.g. leads to a network search
[[ $unc && $root == / ]] && break
if [[ -f $root/.python-version ]]; then
echo "$root/.python-version"
return 0
fi
[ -n "$root" ] || break
[[ -n $root ]] || break
root="${root%/*}"
done
return 1

View file

@ -73,3 +73,15 @@ create_file() {
run pyenv-version-file "$PWD"
assert_failure ""
}
@test "walks up beyond cwd for a relative path" {
create_file ".python-version"
mkdir -p project/subdir
cd project
# Run with CPU-time limit because this used to loop forever
run bash -c 'ulimit -t 5; pyenv-version-file .'
assert_success "${PYENV_TEST_DIR}/.python-version"
run pyenv-version-file ./subdir
assert_success "${PYENV_TEST_DIR}/.python-version"
}