Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard against StackoverflowErrors: prevent packages from extending FileIO.load or FileIO.save #154

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ function load{F}(q::Formatted{F}, args...; options...)
if !has_method_from(methods(Library.load), Library)
throw(LoaderError(string(library), "load not defined"))
end
if Library.load == FileIO.load
throw(LoaderError(string(library), "module should not extend FileIO.load"))
end
return eval(Main, :($(Library.load)($q, $args...; $options...)))
catch e
push!(failures, (e, q))
Expand All @@ -112,6 +115,9 @@ function save{F}(q::Formatted{F}, data...; options...)
if !has_method_from(methods(Library.save), Library)
throw(WriterError(string(library), "save not defined"))
end
if Library.save == FileIO.save
throw(WriterError(string(library), "module should not extend FileIO.save"))
end
return eval(Main, :($(Library.save)($q, $data...; $options...)))
catch e
push!(failures, (e, q))
Expand Down
43 changes: 43 additions & 0 deletions test/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,46 @@ end
@testset "Absent file" begin
@test_throws SystemError load("nonexistent.oops")
end

# Stack overflows (issue #141)
module IOrecursion1
using FileIO
# One shouldn't extend FileIO.load, but what happens if someone does anyway?
FileIO.load(file::File{format"IOrecursion1"}) = 1
end

module IOrecursion2
using FileIO
# One shouldn't extend FileIO.load, but what happens if someone does anyway?
FileIO.load(file::Stream{format"IOrecursion2"}) = 2
end

module IOrecursion3
using FileIO
# One shouldn't extend FileIO.load, but what happens if someone does anyway?
FileIO.load(file::File{format"IOrecursion3"}) = 3
FileIO.load(file::Stream{format"IOrecursion3"}) = 3
end

sym2loader = copy(FileIO.sym2loader)
sym2saver = copy(FileIO.sym2saver)
try
empty!(FileIO.sym2loader)
empty!(FileIO.sym2saver)
add_loader(format"IOrecursion1", :IOrecursion1)
add_loader(format"IOrecursion2", :IOrecursion2)
add_loader(format"IOrecursion3", :IOrecursion3)

@testset "Stackoverflow" begin
io = IOBuffer()
@test load(File(format"IOrecursion1", @__FILE__)) == 1
@test_throws FileIO.LoaderError load(Stream(format"IOrecursion1", io))
@test_throws FileIO.LoaderError load(File(format"IOrecursion2", @__FILE__))
@test load(Stream(format"IOrecursion2", io)) == 2
@test load(File(format"IOrecursion3", @__FILE__)) == 3
@test load(Stream(format"IOrecursion3", io)) == 3
end
finally
merge!(FileIO.sym2loader, sym2loader)
merge!(FileIO.sym2saver, sym2saver)
end