Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integer overflow in skip(s::IOBuffer, typemax(Int64)) #54070

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,22 +260,32 @@ bytesavailable(io::GenericIOBuffer) = io.size - io.ptr + 1
position(io::GenericIOBuffer) = io.ptr - io.offset - 1

function skip(io::GenericIOBuffer, n::Integer)
seekto = io.ptr + n
nhz2 marked this conversation as resolved.
Show resolved Hide resolved
n < 0 && return seek(io, seekto-1) # Does error checking
io.ptr = min(seekto, io.size+1)
return io
skip(io, clamp(n, Int))
end
function skip(io::GenericIOBuffer, n::Int)
if signbit(n)
seekto = clamp(widen(position(io)) + widen(n), Int)
seek(io, seekto) # Does error checking
else
n_max = io.size + 1 - io.ptr
io.ptr += min(n, n_max)
io
end
end

function seek(io::GenericIOBuffer, n::Integer)
seek(io, clamp(n, Int))
end
function seek(io::GenericIOBuffer, n::Int)
if !io.seekable
ismarked(io) || throw(ArgumentError("seek failed, IOBuffer is not seekable and is not marked"))
n == io.mark || throw(ArgumentError("seek failed, IOBuffer is not seekable and n != mark"))
end
# TODO: REPL.jl relies on the fact that this does not throw (by seeking past the beginning or end
# of an GenericIOBuffer), so that would need to be fixed in order to throw an error here
#(n < 0 || n > io.size) && throw(ArgumentError("Attempted to seek outside IOBuffer boundaries."))
#io.ptr = n+1
io.ptr = min(max(0, n)+io.offset, io.size)+1
#(n < 0 || n > io.size - io.offset) && throw(ArgumentError("Attempted to seek outside IOBuffer boundaries."))
#io.ptr = n + io.offset + 1
io.ptr = clamp(n, 0, io.size - io.offset) + io.offset + 1
return io
end

Expand Down
25 changes: 25 additions & 0 deletions test/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,31 @@ end
@test position(skip(io, -3)) == 0
end

@testset "issue #53908" begin
@testset "offset $first" for first in (false, true)
b = collect(0x01:0x05)
sizehint!(b, 100; first) # make offset non zero
io = IOBuffer(b)
@test position(skip(io, 4)) == 4
@test position(skip(io, typemax(Int))) == 5
@test position(skip(io, typemax(Int128))) == 5
@test position(skip(io, typemax(Int32))) == 5
@test position(skip(io, typemin(Int))) == 0
@test position(skip(io, typemin(Int128))) == 0
@test position(skip(io, typemin(Int32))) == 0
@test position(skip(io, 4)) == 4
@test position(skip(io, -2)) == 2
@test position(skip(io, -2)) == 0
@test position(seek(io, -2)) == 0
@test position(seek(io, typemax(Int))) == 5
@test position(seek(io, typemax(Int128))) == 5
@test position(seek(io, typemax(Int32))) == 5
@test position(seek(io, typemin(Int))) == 0
@test position(seek(io, typemin(Int128))) == 0
@test position(seek(io, typemin(Int32))) == 0
end
end

@testset "pr #11554" begin
io = IOBuffer(SubString("***αhelloworldω***", 4, 16))
io2 = IOBuffer(Vector{UInt8}(b"goodnightmoon"), read=true, write=true)
Expand Down