Skip to content

Commit

Permalink
avoid overflow in nfield loading
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasIsensee committed May 13, 2024
1 parent 7579dfb commit dd3b2d5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.4.47
- fix loading structs with more than 256 fields (#558)

## 0.4.46
- remove usage of `IOBuffer` internals

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "JLD2"
uuid = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
version = "0.4.46"
version = "0.4.47"

[deps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Expand Down
2 changes: 1 addition & 1 deletion src/datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ end
function jlread(io::IO, ::Type{CompoundDatatype})
dt = jlread(io, BasicDatatype)
version = dt.class >> 4
nfields = UInt16(dt.bitfield1) | UInt16(dt.bitfield2 << 8)
nfields = UInt16(dt.bitfield1) | UInt16(dt.bitfield2) << 8
dt.bitfield3 == 0 || throw(UnsupportedFeatureException())

names = Vector{Symbol}(undef, nfields)
Expand Down
15 changes: 15 additions & 0 deletions test/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -749,4 +749,19 @@ end
@test x == f["x"]
end
end
end

@testset "Issue #558 a struct with 256 fields" begin
fields = [Symbol("field",i) for i in 1:256]
@eval struct Structwithmanyfields
$(fields...)
end
# Fill the struct
obj = Structwithmanyfields(ntuple(i -> i, 256)...)

cd(mktempdir()) do
save_object("myStruct.jld2", obj)
loaded = load_object("myStruct.jld2")
@test loaded isa Structwithmanyfields
end
end

0 comments on commit dd3b2d5

Please sign in to comment.