From 3e4282fca5285b06cdbb87d29cf516226b5922dd Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Mon, 7 Jul 2025 22:19:58 +0200 Subject: [PATCH] Fix stack overflow in `safe_realpath` Fixes https://github.com/JuliaLang/Pkg.jl/issues/3085 That issue was already closed by https://github.com/JuliaLang/Pkg.jl/pull/3087 but I still ran into the same issue, it seems the fix was wrong. The stack overflow can still be triggered on Windows by ```jl using Pkg: safe_realpath safe_realpath("some-non-existing-drive:") ``` It keeps on recursing trying to make it smaller with `splitdir`, but it can't. We know we cannot make it smaller if the returned "filename" is empty. On Linux we have: ```jl julia> splitdir("/") ("/", "") ``` On Windows: ```jl julia> splitdir("C://") ("C:/", "") ``` --- src/utils.jl | 3 ++- test/misc.jl | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 2b652d7f80..45df5dc6c5 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -119,7 +119,6 @@ end # try to call realpath on as much as possible function safe_realpath(path) - isempty(path) && return path if ispath(path) try return realpath(path) @@ -128,6 +127,8 @@ function safe_realpath(path) end end a, b = splitdir(path) + # path cannot be reduced at the root or drive, avoid stack overflow + isempty(b) && return path return joinpath(safe_realpath(a), b) end diff --git a/test/misc.jl b/test/misc.jl index e9b3d00ff6..3a386d161a 100644 --- a/test/misc.jl +++ b/test/misc.jl @@ -19,8 +19,9 @@ end end @testset "safe_realpath" begin + realpath(Sys.BINDIR) == Pkg.safe_realpath(Sys.BINDIR) # issue #3085 - for p in ("", "some-non-existing-path") + for p in ("", "some-non-existing-path", "some-non-existing-drive:") @test p == Pkg.safe_realpath(p) end end