Protect against invalidated iterator in radar product manager

This commit is contained in:
Dan Paulat 2023-06-10 23:00:42 -05:00
parent ca61ed257d
commit 580534d396

View file

@ -1192,15 +1192,17 @@ void RadarProductManagerImpl::UpdateRecentRecords(
std::shared_ptr<types::RadarProductRecord> record) std::shared_ptr<types::RadarProductRecord> record)
{ {
const std::size_t recentListMaxSize {cacheLimit_}; const std::size_t recentListMaxSize {cacheLimit_};
bool iteratorErased = false;
auto it = std::find(recentList.cbegin(), recentList.cend(), record); auto it = std::find(recentList.cbegin(), recentList.cend(), record);
if (it != recentList.cbegin() && it != recentList.cend()) if (it != recentList.cbegin() && it != recentList.cend())
{ {
// If the record exists beyond the front of the list, remove it // If the record exists beyond the front of the list, remove it
recentList.erase(it); recentList.erase(it);
iteratorErased = true;
} }
if (recentList.size() == 0 || it != recentList.cbegin()) if (iteratorErased || recentList.size() == 0 || it != recentList.cbegin())
{ {
// Add the record to the front of the list, unless it's already there // Add the record to the front of the list, unless it's already there
recentList.push_front(record); recentList.push_front(record);