Skip to content

add check for invalid state in _growend! slow-path #53513

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

Merged
merged 6 commits into from
Mar 11, 2024
Merged
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
13 changes: 13 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,9 @@ function _growbeg!(a::Vector, delta::Integer)
else
@noinline (function()
memlen = length(mem)
if offset + len - 1 > memlen || offset < 1
throw(ConcurrencyViolationError("Vector has invalid state. Don't modify internal fields incorrectly, or resize without correct locks"))
end
# since we will allocate the array in the middle of the memory we need at least 2*delta extra space
# the +1 is because I didn't want to have an off by 1 error.
newmemlen = max(overallocation(memlen), len + 2 * delta + 1)
Expand All @@ -1083,6 +1086,9 @@ function _growbeg!(a::Vector, delta::Integer)
newmem = array_new_memory(mem, newmemlen)
end
unsafe_copyto!(newmem, newoffset + delta, mem, offset, len)
if ref !== a.ref
@noinline throw(ConcurrencyViolationError("Vector can not be resized concurrently"))
end
setfield!(a, :ref, @inbounds GenericMemoryRef(newmem, newoffset))
end)()
end
Expand All @@ -1103,6 +1109,10 @@ function _growend!(a::Vector, delta::Integer)
newmemlen = offset + newlen - 1
if memlen < newmemlen
@noinline (function()
if offset + len - 1 > memlen || offset < 1
throw(ConcurrencyViolationError("Vector has invalid state. Don't modify internal fields incorrectly, or resize without correct locks"))
end

if offset - 1 > div(5 * newlen, 4)
# If the offset is far enough that we can copy without resizing
# while maintaining proportional spacing on both ends of the array
Expand All @@ -1120,6 +1130,9 @@ function _growend!(a::Vector, delta::Integer)
end
newref = @inbounds GenericMemoryRef(newmem, newoffset)
unsafe_copyto!(newref, ref, len)
if ref !== a.ref
@noinline throw(ConcurrencyViolationError("Vector can not be resized concurrently"))
end
setfield!(a, :ref, newref)
end)()
end
Expand Down