Skip to content

Commit

Permalink
Fix variable length strings as attributes
Browse files Browse the repository at this point in the history
Fixes #1129
  • Loading branch information
simonbyrne committed Nov 1, 2023
1 parent ddc33ec commit 724c0ed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,13 @@ end
function write_attribute(attr::Attribute, memtype::Datatype, str::AbstractString)
strbuf = Base.cconvert(Cstring, str)
GC.@preserve strbuf begin
buf = Base.unsafe_convert(Ptr{UInt8}, strbuf)
write_attribute(attr, memtype, buf)
if API.h5t_is_variable_str(memtype)
ptr = Base.unsafe_convert(Cstring, strbuf)
write_attribute(attr, memtype, Ref(ptr))
else
ptr = Base.unsafe_convert(Ptr{UInt8}, strbuf)
write_attribute(attr, memtype, ptr)
end
end
end
function write_attribute(
Expand Down
10 changes: 10 additions & 0 deletions test/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,13 @@ end
close(f)
end
end

@testset "variable length strings" begin
filename = tempname()
h5open(filename, "w") do f
# https://github.com/JuliaIO/HDF5.jl/issues/1129
attr = create_attribute(f, "attr-name", datatype(String), dataspace(String))
write_attribute(attr, datatype(String), "attr-value")
@test attrs(f)["attr-name"] == "attr-value"
end
end

0 comments on commit 724c0ed

Please sign in to comment.