Skip to content

Commit e81cd34

Browse files
authored
Remove unneeded Base.unsafe_read NoopStream method (#199)
1 parent 5bcbdcc commit e81cd34

File tree

4 files changed

+66
-23
lines changed

4 files changed

+66
-23
lines changed

fuzz/fuzz.jl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,40 @@ function wrap_stream(codecs_kws, io::IO)::IO
5858
end
5959
end
6060

61+
# Read data using various means.
62+
# These function read a vector of bytes from io,
63+
# if io is eof they should return an empty vector.
64+
read_methods = Data.SampledFrom([
65+
function read_byte(io)
66+
eof(io) && return UInt8[]
67+
[read(io, UInt8)]
68+
end,
69+
function read_by_readuntil_keep(io)
70+
delim = 0x01
71+
readuntil(io, delim; keep=true)
72+
end,
73+
function read_by_Base_unsafe_read(io)
74+
n = bytesavailable(io)
75+
ret = zeros(UInt8, n)
76+
GC.@preserve ret Base.unsafe_read(io, pointer(ret), n)
77+
ret
78+
end,
79+
function read_by_readavailable(io)
80+
readavailable(io)
81+
end,
82+
function read_by_readbytes1(io)
83+
ret = zeros(UInt8, 10000)
84+
n = readbytes!(io, ret)
85+
ret[1:n]
86+
end,
87+
function read_by_readbytes2(io)
88+
ret = zeros(UInt8, 0)
89+
n = readbytes!(io, ret, 10000)
90+
ret[1:n]
91+
end
92+
])
93+
94+
6195
@check function read_byte_data(
6296
kws=read_codecs_kws,
6397
data=datas,
@@ -76,6 +110,21 @@ end
76110
read(stream) == data || return false
77111
eof(stream)
78112
end
113+
@check function read_data_methods(
114+
kws=read_codecs_kws,
115+
data=datas,
116+
rs=Data.Vectors(read_methods),
117+
)
118+
stream = wrap_stream(kws, IOBuffer(data))
119+
x = UInt8[]
120+
for r in rs
121+
d = r(stream)
122+
append!(x, d)
123+
# TODO fix position
124+
# length(x) == position(stream) || return false
125+
end
126+
x == data[eachindex(x)]
127+
end
79128

80129
# flush all nested streams and return final data
81130
function take_all(stream)

src/noop.jl

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,6 @@ function Base.seekend(stream::NoopStream)
9797
return stream
9898
end
9999

100-
function Base.unsafe_read(stream::NoopStream, output::Ptr{UInt8}, nbytes::UInt)
101-
changemode!(stream, :read)
102-
buffer = stream.buffer1
103-
p = output
104-
p_end = output + nbytes
105-
while p < p_end && !eof(stream)
106-
if buffersize(buffer) > 0
107-
m = min(buffersize(buffer), p_end - p)
108-
copydata!(p, buffer, m)
109-
else
110-
# directly read data from the underlying stream
111-
m = p_end - p
112-
Base.unsafe_read(stream.stream, p, m)
113-
end
114-
p += m
115-
end
116-
if p < p_end && eof(stream)
117-
throw(EOFError())
118-
end
119-
return
120-
end
121-
122100
function Base.unsafe_write(stream::NoopStream, input::Ptr{UInt8}, nbytes::UInt)
123101
changemode!(stream, :write)
124102
buffer = stream.buffer1

src/stream.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ function Base.unsafe_read(stream::TranscodingStream, output::Ptr{UInt8}, nbytes:
400400
p += m
401401
GC.safepoint()
402402
end
403-
if p < p_end && eof(stream)
403+
if p < p_end
404404
throw(EOFError())
405405
end
406406
return

test/codecdoubleframe.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,22 @@ DoubleFrameDecoderStream(stream::IO; kwargs...) = TranscodingStream(DoubleFrameD
284284
@test eof(s2)
285285
end
286286

287+
@testset "reading zero bytes from invalid stream" begin
288+
# This behavior is required to avoid breaking JLD2.jl
289+
# `s` must go into read mode, but not actually call `eof`
290+
for readnone in (io -> read!(io, UInt8[]), io -> read(io, 0))
291+
for invalid_data in (b"", b"asdf")
292+
s = DoubleFrameDecoderStream(IOBuffer(invalid_data;read=true,write=true))
293+
@test iswritable(s)
294+
@test isreadable(s)
295+
readnone(s)
296+
@test !iswritable(s)
297+
@test isreadable(s)
298+
@test_throws ErrorException eof(s)
299+
end
300+
end
301+
end
302+
287303
test_roundtrip_read(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
288304
test_roundtrip_write(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
289305
test_roundtrip_lines(DoubleFrameEncoderStream, DoubleFrameDecoderStream)

0 commit comments

Comments
 (0)