Avoid thresholds vector update while iterating
new data is pushed on thresholds vector while it is being iterated.
vector insertion can resize vector length. Resize in vector cause
invalidation of iterators and further operation iterator is undefined
behaviour(i.e. crash).
Iterate through vector by indexing it from beginning to original size.
Tested:
No cashes were observed with thresholds vector iteration.
Change-Id: Idf1d017847fa77128922a38eb6e33e0fa5702f3d
Signed-off-by: Vikash Chandola <vikash.chandola@intel.com>
diff --git a/src/sensor.hpp b/src/sensor.hpp
index 17ed488..9f092c6 100644
--- a/src/sensor.hpp
+++ b/src/sensor.hpp
@@ -549,8 +549,10 @@
// optional.
void fillMissingThresholds()
{
- for (thresholds::Threshold& thisThreshold : thresholds)
+ const std::size_t thresholdsLen = thresholds.size();
+ for (std::size_t index = 0; index < thresholdsLen; ++index)
{
+ const thresholds::Threshold& thisThreshold = thresholds[index];
bool foundOpposite = false;
thresholds::Direction opposite = thresholds::Direction::HIGH;
if (thisThreshold.direction == thresholds::Direction::HIGH)