Skip to content

Commit 031c35c

Browse files
fredrikekreKristofferC
authored andcommitted
fix #31758: out of bounds write in sparse broadcast (#31763)
(cherry picked from commit c0c6f96)
1 parent 0ed3903 commit 031c35c

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

stdlib/SparseArrays/src/higherorderfns.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ function _map_zeropres!(f::Tf, C::SparseVecOrMat, As::Vararg{SparseVecOrMat,N})
383383
vals, ks, rows = _fusedupdate_all(rowsentinel, activerow, rows, ks, stopks, As)
384384
Cx = f(vals...)
385385
if !_iszero(Cx)
386-
Ck > spaceC && (spaceC = expandstorage!(C, Ck + min(length(C), _sumnnzs(As...)) - (sum(ks) - N)))
386+
Ck > spaceC && (spaceC = expandstorage!(C, min(length(C), Ck + _sumnnzs(As...) - (sum(ks) - N))))
387387
storedinds(C)[Ck] = activerow
388388
storedvals(C)[Ck] = Cx
389389
Ck += 1

stdlib/SparseArrays/test/higherorderfns.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,4 +632,14 @@ end
632632
@test minimum(sparse([1, 2], [1, 2], ones(Int32, 2)), dims = 1) isa Matrix
633633
end
634634

635+
@testset "issue #31758: out of bounds write in _map_zeropres!" begin
636+
y = sparsevec([2,7], [1., 2.], 10)
637+
x1 = sparsevec(fill(1.0, 10))
638+
x2 = sparsevec([2,7], [1., 2.], 10)
639+
x3 = sparsevec(fill(1.0, 10))
640+
f(x, y, z) = x == y == z == 0 ? 0.0 : NaN
641+
y .= f.(x1, x2, x3)
642+
@test all(isnan, y)
643+
end
644+
635645
end # module

0 commit comments

Comments
 (0)