diff options
author | 2023-08-22 11:56:30 -0700 | |
---|---|---|
committer | 2023-08-22 11:56:30 -0700 | |
commit | eb0f532d94f920c888021e52d98cdebe79cd37e7 (patch) | |
tree | 5255299090e4426c5c657da183c99d179715fc13 /Source/Utils/Parser/IntervalsParser.cpp | |
parent | c7fdab4691686b09a4af67252793efb7b5f046d1 (diff) | |
download | WarpX-eb0f532d94f920c888021e52d98cdebe79cd37e7.tar.gz WarpX-eb0f532d94f920c888021e52d98cdebe79cd37e7.tar.zst WarpX-eb0f532d94f920c888021e52d98cdebe79cd37e7.zip |
fix undefined behavior in btd intervals (#4216)
Diffstat (limited to '')
-rw-r--r-- | Source/Utils/Parser/IntervalsParser.cpp | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/Source/Utils/Parser/IntervalsParser.cpp b/Source/Utils/Parser/IntervalsParser.cpp index 1a0f01f88..dd72ba900 100644 --- a/Source/Utils/Parser/IntervalsParser.cpp +++ b/Source/Utils/Parser/IntervalsParser.cpp @@ -193,12 +193,12 @@ utils::parser::BTDIntervalsParser::BTDIntervalsParser ( const int start = temp_slice.getStart(); const int period = temp_slice.getPeriod(); int btd_iter_ind; - // for Slice temp_slice in m_slices, - // determine the index in m_btd_iterations where temp_slice's starting value goes - // // Implementation note: - // assuming the user mostly lists slices in ascending order, // start at the end of m_btd_iterations and search backward + // with the thinking that the user mostly lists slices in ascending order + // + // for Slice temp_slice in m_slices, + // determine the largest index in m_btd_iterations smaller than start if (m_btd_iterations.size() == 0) { btd_iter_ind = 0; @@ -206,7 +206,7 @@ utils::parser::BTDIntervalsParser::BTDIntervalsParser ( else { btd_iter_ind = m_btd_iterations.size() - 1; - while (start < m_btd_iterations[btd_iter_ind] and btd_iter_ind>0) + while (start < m_btd_iterations.at(btd_iter_ind) and btd_iter_ind>0) { btd_iter_ind--; } @@ -214,26 +214,42 @@ utils::parser::BTDIntervalsParser::BTDIntervalsParser ( // insert each iteration contained in temp_slice into m_btd_iterations // adding them in increasing sorted order and not adding any iterations // already contained in m_btd_iterations - for (int ii = start; ii <= temp_slice.getStop(); ii += period) + for (int slice_iter = start; slice_iter <= temp_slice.getStop(); slice_iter += period) { - if (m_btd_iterations.size() > 0) + // number of iterations currently in this slice + auto const num_btd_iterations = static_cast<int>(m_btd_iterations.size()); + + if (num_btd_iterations > 0) { - // find where iteration ii should go in m_btd_iterations - while (ii > m_btd_iterations[btd_iter_ind] && btd_iter_ind < static_cast<int>(m_btd_iterations.size())) + // increment btd_iter_ind for each existing iteration, + // if slice_iter is larger than an existing one + while (btd_iter_ind < num_btd_iterations && + slice_iter > m_btd_iterations.at(btd_iter_ind)) { btd_iter_ind++; } - if (ii != m_btd_iterations[btd_iter_ind]) + // this is the place to insert slice_iter if it is not in m_btd_iterations + // if slice_iter > all entries in m_btd_iterations, append slice_iter + if (btd_iter_ind == num_btd_iterations) { - m_btd_iterations.insert(m_btd_iterations.begin() + btd_iter_ind, ii); + m_btd_iterations.insert(m_btd_iterations.begin() + btd_iter_ind, slice_iter); + } else + { + if (slice_iter != m_btd_iterations.at(btd_iter_ind)) + { + m_btd_iterations.insert(m_btd_iterations.begin() + btd_iter_ind, slice_iter); + } } } else { - m_btd_iterations.push_back(ii); + m_btd_iterations.push_back(slice_iter); } } - if ((temp_slice.getPeriod() > 0) && - (temp_slice.getStop() >= start)) m_activated = true; + if (temp_slice.getPeriod() > 0 && + temp_slice.getStop() >= start) + { + m_activated = true; + } } } |