Skip to content

Commit 62b4396

Browse files
committed
Fix ASAN warnings in __ilu__.cc (bug #67249)
* __ilu__.cc (ilu_tp): Don't dereference iterator before checking that it has gone past end. Use C+11 form of std::set::erase() which returns valid iterator to element after the one being deleted to simplify code. Increment iterator at bottom of loop, but do not dereference. Use std::swap() to simplify code.
1 parent 9f376a4 commit 62b4396

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

libinterp/corefcn/__ilu__.cc

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -614,10 +614,13 @@ ilu_tp (octave_matrix_t& sm, octave_matrix_t& L, octave_matrix_t& U,
614614
}
615615

616616
it = iw_u.begin ();
617-
jrow = *it;
618617
total_sum = zero;
619-
while ((jrow < k) && (it != iw_u.end ()))
618+
while (it != iw_u.end ())
620619
{
620+
jrow = *it;
621+
if (jrow >= k)
622+
break;
623+
621624
if (opt == COL)
622625
partial_col_sum = w_data[jrow];
623626
if (w_data[jrow] != zero)
@@ -663,10 +666,7 @@ ilu_tp (octave_matrix_t& sm, octave_matrix_t& L, octave_matrix_t& U,
663666
else if (opt == ROW)
664667
total_sum += partial_row_sum;
665668
w_data[jrow] = zero;
666-
it2 = it;
667-
it++;
668-
iw_u.erase (it2);
669-
jrow = *it;
669+
it = iw_u.erase (it); // Returns next valid iterator
670670
continue;
671671
}
672672
else
@@ -675,7 +675,7 @@ ilu_tp (octave_matrix_t& sm, octave_matrix_t& L, octave_matrix_t& U,
675675
if (opt == ROW)
676676
w_data[jrow] = tl;
677677
}
678-
jrow = *(++it);
678+
++it; // Increment only when we did not erase
679679
}
680680

681681
// Search for the pivot and update iw_l and iw_u if the pivot is not the
@@ -703,15 +703,11 @@ ilu_tp (octave_matrix_t& sm, octave_matrix_t& L, octave_matrix_t& U,
703703
iw_l.insert (perm[k]);
704704
else
705705
iw_u.insert (k);
706-
// Swap data and update permutation vectors
707-
aux = w_data[k];
706+
// Update permutation vectors and swap data
708707
iperm[perm[p_perm]] = k;
709708
iperm[perm[k]] = p_perm;
710-
c = perm[k];
711-
perm[k] = perm[p_perm];
712-
perm[p_perm] = c;
713-
w_data[k] = w_data[p_perm];
714-
w_data[p_perm] = aux;
709+
std::swap (perm[k], perm[p_perm]);
710+
std::swap (w_data[k], w_data[p_perm]);
715711
}
716712

717713
}
@@ -728,9 +724,7 @@ ilu_tp (octave_matrix_t& sm, octave_matrix_t& L, octave_matrix_t& U,
728724
if (opt != OFF)
729725
total_sum += w_data[p_perm];
730726
w_data[p_perm] = zero;
731-
it2 = it;
732-
it++;
733-
iw_l.erase (it2);
727+
it = iw_l.erase (it);
734728
continue;
735729
}
736730

0 commit comments

Comments
 (0)