Skip to content

Commit 6477530

Browse files
authored
prevent stackoverflow of stat/lstat (#55554)
Gives a better error message if joinpath does not change types (which will cause stat/lstat to resolve to the same method and crash). Fixes #50890
1 parent 03451ff commit 6477530

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

base/stat.jl

+4
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ end
198198

199199
"""
200200
stat(file)
201+
stat(joinpath...)
201202
202203
Return a structure whose fields contain information about the file.
203204
The fields of the structure are:
@@ -218,16 +219,19 @@ The fields of the structure are:
218219
| mtime | `Float64` | Unix timestamp of when the file was last modified |
219220
| ctime | `Float64` | Unix timestamp of when the file's metadata was changed |
220221
"""
222+
stat(path) = (path2 = joinpath(path); path2 isa typeof(path) ? error("stat not implemented for $(typeof(path))") : stat(path2))
221223
stat(path...) = stat(joinpath(path...))
222224

223225
"""
224226
lstat(file)
227+
lstat(joinpath...)
225228
226229
Like [`stat`](@ref), but for symbolic links gets the info for the link
227230
itself rather than the file it refers to.
228231
This function must be called on a file path rather than a file object or a file
229232
descriptor.
230233
"""
234+
lstat(path) = (path2 = joinpath(path); path2 isa typeof(path) ? error("lstat not implemented for $(typeof(path))") : lstat(path2))
231235
lstat(path...) = lstat(joinpath(path...))
232236

233237
# some convenience functions

test/file.jl

+10
Original file line numberDiff line numberDiff line change
@@ -1753,8 +1753,18 @@ end
17531753
@test s.blocks isa Int64
17541754
@test s.mtime isa Float64
17551755
@test s.ctime isa Float64
1756+
1757+
@test s === stat((f,))
1758+
@test s === lstat((f,))
1759+
@test s === stat(".", f)
1760+
@test s === lstat(".", f)
17561761
end
17571762

1763+
mutable struct URI50890; f::String; end
1764+
Base.joinpath(x::URI50890) = URI50890(x.f)
1765+
@test_throws "stat not implemented" stat(URI50890("."))
1766+
@test_throws "lstat not implemented" lstat(URI50890("."))
1767+
17581768
@testset "StatStruct show's extended details" begin
17591769
f, io = mktemp()
17601770
s = stat(f)

0 commit comments

Comments
 (0)