Skip to content

GenericIOBuffer assumes data is stored contiguously in memory #54636

Closed
@nhz2

Description

@nhz2

julia/base/iobuffer.jl

Lines 184 to 194 in 4ecdc37

function unsafe_read(from::GenericIOBuffer, p::Ptr{UInt8}, nb::UInt)
from.readable || _throw_not_readable()
avail = bytesavailable(from)
adv = min(avail, nb)
GC.@preserve from unsafe_copyto!(p, pointer(from.data, from.ptr), adv)
from.ptr += adv
if nb > avail
throw(EOFError())
end
nothing
end

This can lead to the following strange behavior:

julia> a = @view(collect(0x00:0x0f)[begin:2:end])
8-element view(::Vector{UInt8}, 1:2:15) with eltype UInt8:
 0x00
 0x02
 0x04
 0x06
 0x08
 0x0a
 0x0c
 0x0e

julia> b = read(IOBuffer(a))
8-element Vector{UInt8}:
 0x00
 0x01
 0x02
 0x03
 0x04
 0x05
 0x06
 0x07
### Tasks

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviorioInvolving the I/O subsystem: libuv, read, write, etc.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions