From dd3b2d51a9612d132866df31125f474d3f83ce64 Mon Sep 17 00:00:00 2001 From: Jonas Isensee Date: Mon, 13 May 2024 08:45:53 +0200 Subject: [PATCH] avoid overflow in nfield loading --- CHANGELOG.md | 3 +++ Project.toml | 2 +- src/datatypes.jl | 2 +- test/loadsave.jl | 15 +++++++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8271e426..57a60500 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.4.47 + - fix loading structs with more than 256 fields (#558) + ## 0.4.46 - remove usage of `IOBuffer` internals diff --git a/Project.toml b/Project.toml index 21a93a92..2af0724f 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/datatypes.jl b/src/datatypes.jl index ac1924be..0a65441d 100644 --- a/src/datatypes.jl +++ b/src/datatypes.jl @@ -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) diff --git a/test/loadsave.jl b/test/loadsave.jl index 0ca9204b..4098fdc6 100644 --- a/test/loadsave.jl +++ b/test/loadsave.jl @@ -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 \ No newline at end of file