Skip to content

Commit c0c6f96

Browse files
authored
fix #31758: out of bounds write in sparse broadcast (#31763)
1 parent 32b091b commit c0c6f96

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
@@ -387,7 +387,7 @@ function _map_zeropres!(f::Tf, C::SparseVecOrMat, As::Vararg{SparseVecOrMat,N})
387387
vals, ks, rows = _fusedupdate_all(rowsentinel, activerow, rows, ks, stopks, As)
388388
Cx = f(vals...)
389389
if !_iszero(Cx)
390-
Ck > spaceC && (spaceC = expandstorage!(C, Ck + min(length(C), _sumnnzs(As...)) - (sum(ks) - N)))
390+
Ck > spaceC && (spaceC = expandstorage!(C, min(length(C), Ck + _sumnnzs(As...) - (sum(ks) - N))))
391391
storedinds(C)[Ck] = activerow
392392
storedvals(C)[Ck] = Cx
393393
Ck += 1

stdlib/SparseArrays/test/higherorderfns.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,4 +671,14 @@ end
671671
@test w == bv .* op(av)
672672
end
673673

674+
@testset "issue #31758: out of bounds write in _map_zeropres!" begin
675+
y = sparsevec([2,7], [1., 2.], 10)
676+
x1 = sparsevec(fill(1.0, 10))
677+
x2 = sparsevec([2,7], [1., 2.], 10)
678+
x3 = sparsevec(fill(1.0, 10))
679+
f(x, y, z) = x == y == z == 0 ? 0.0 : NaN
680+
y .= f.(x1, x2, x3)
681+
@test all(isnan, y)
682+
end
683+
674684
end # module

0 commit comments

Comments
 (0)