Fixed sparse conservativeResize() when both num cols and rows decreased.

The previous implementation caused a buffer overflow trying to calculate non-
zero counts for columns that no longer exist.
This commit is contained in:
Adam Shapiro
2021-02-23 21:32:39 +00:00
committed by Antonio Sánchez
parent 10c77b0ff4
commit 2ac0b78739
2 changed files with 19 additions and 9 deletions

View File

@@ -579,10 +579,12 @@ class SparseMatrix
else if (innerChange < 0)
{
// Inner size decreased: allocate a new m_innerNonZeros
m_innerNonZeros = static_cast<StorageIndex*>(std::malloc((m_outerSize+outerChange+1) * sizeof(StorageIndex)));
m_innerNonZeros = static_cast<StorageIndex*>(std::malloc((m_outerSize + outerChange) * sizeof(StorageIndex)));
if (!m_innerNonZeros) internal::throw_std_bad_alloc();
for(Index i = 0; i < m_outerSize; i++)
for(Index i = 0; i < m_outerSize + (std::min)(outerChange, Index(0)); i++)
m_innerNonZeros[i] = m_outerIndex[i+1] - m_outerIndex[i];
for(Index i = m_outerSize; i < m_outerSize + outerChange; i++)
m_innerNonZeros[i] = 0;
}
// Change the m_innerNonZeros in case of a decrease of inner size