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
parent c5537e23
......@@ -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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment