Skip to content

Commit da99461

Browse files
authored
Fix use of pointer in write(to::IO, from::GenericIOBuffer) (#57949)
1 parent 7341098 commit da99461

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

base/iobuffer.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,8 +811,8 @@ function write(to::IO, from::GenericIOBuffer)
811811
if to === from
812812
throw(ArgumentError("Writing all content fron an IOBuffer into itself in invalid"))
813813
else
814-
available = bytesavailable(from)
815-
written = GC.@preserve from unsafe_write(to, pointer(from.data, from.ptr), UInt(available))
814+
from.readable || _throw_not_readable()
815+
written = write(to, view(from.data, from.ptr:from.size))
816816
from.ptr = from.size + 1
817817
end
818818
return written

test/iobuffer.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ end
265265
write(to, from)
266266
@test String(take!(to)) == "abcd"
267267
@test eof(from)
268+
269+
# Write to another IOBuffer when closed
270+
to = IOBuffer()
271+
from = IOBuffer(collect(b"abcdefghi"))
272+
close(from)
273+
@test_throws ArgumentError write(to, from)
268274
end
269275

270276
@testset "Read/write empty IOBuffer" begin
@@ -701,6 +707,11 @@ end
701707
data = 0x00:0xFF
702708
io = IOBuffer(data)
703709
@test read(io) == data
710+
seekstart(io)
711+
@test read(io, UInt16) === ltoh(0x0100)
712+
out = IOBuffer()
713+
write(out, io)
714+
@test take!(out) == data[3:end]
704715

705716
data = @view(collect(0x00:0x0f)[begin:2:end])
706717
io = IOBuffer(data)

0 commit comments

Comments
 (0)