diff --git a/src/attributes.jl b/src/attributes.jl index 0c683224..9af867dd 100644 --- a/src/attributes.jl +++ b/src/attributes.jl @@ -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( diff --git a/test/attributes.jl b/test/attributes.jl index b2a71028..4d7111b2 100644 --- a/test/attributes.jl +++ b/test/attributes.jl @@ -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