Bytecode: fix logic in routine that substracts two sparse matrices

The logic of the dynSparseMatrix::Sparse_substract_SA_SB() routine was
incorrect.

In some cases, it would read past the last nonzero elements of the A matrix,
and consequently write past the number of allocated nonzero elements of the C
matrix.

This would lead to crashes and, probably, to wrong results under certain
circumstances.

Closes: #1652
time-shift
Sébastien Villemot 2019-11-27 16:54:01 +01:00
parent c5537e23ae
commit 9e222734a8
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
1 changed files with 2 additions and 2 deletions

View File

@ -2618,7 +2618,7 @@ dynSparseMatrix::Sparse_substract_SA_SB(mxArray *A_m, mxArray *B_m)
C_j[A_col+1] = nze_C++;
C_col = A_col;
}
else if (A_row < B_row || (nze_B >= total_nze_B && nze_A < total_nze_A))
else if ((A_row < B_row && nze_A < total_nze_A) || nze_B == total_nze_B)
{
C_d[nze_C] = A_d[nze_A++];
C_i[nze_C] = A_row;
@ -2637,7 +2637,7 @@ dynSparseMatrix::Sparse_substract_SA_SB(mxArray *A_m, mxArray *B_m)
C_col = B_col;
}
}
else if (A_col < B_col || (nze_B >= total_nze_B && nze_A < total_nze_A))
else if ((A_col < B_col && nze_A < total_nze_A) || nze_B == total_nze_B)
{
C_d[nze_C] = A_d[nze_A++];
C_i[nze_C] = A_row;