Skip to content

Commit

Permalink
Don't keep file in open files cache if it fails to read metadata (#483)
Browse files Browse the repository at this point in the history
* Don't keep file in open files cache if it fails to read metadata

Fixes JuliaPackaging/BinaryBuilder.jl#1292

* Update test/test_files.jl

---------

Co-authored-by: JonasIsensee <[email protected]>
  • Loading branch information
Keno and JonasIsensee authored Sep 20, 2023
1 parent 7aed02d commit 6ae032b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/JLD2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,12 @@ function jldopen(fname::AbstractString, wr::Bool, create::Bool, truncate::Bool,
f.types_group = Group{JLDFile{IOStream}}(f)
end
else
load_file_metadata!(f)
try
load_file_metadata!(f)
catch e
close(f)
rethrow(e)
end
end
merge!(f.typemap, typemap)
return f
Expand Down
12 changes: 12 additions & 0 deletions test/test_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,15 @@ testfiles = artifact"testfiles/JLD2TestFiles-0.1.0/artifacts"

end
end

@testset "Opening corrupted files" begin
mktemp() do path, io
close(io)
@test_throws EOFError jldopen(path)
# We want to make sure that this didn't accidentally add itself to the open
# files cache
@test_throws EOFError jldopen(path)
# Opening for overwriting should succeed
close(jldopen(path, "w+"))
end
end

0 comments on commit 6ae032b

Please sign in to comment.