summaryrefslogtreecommitdiffstats
path: root/storaged/storaged_info.cpp
diff options
context:
space:
mode:
authorDavid Anderson <dvander@google.com>2018-03-26 15:15:05 -0700
committerDavid Anderson <dvander@google.com>2018-03-26 15:53:23 -0700
commitf2dd78bb2c9fe0e6482cf012e26664752fc64401 (patch)
treec6de9cd33c42a8c8ff7badcb712be99cc20104cf /storaged/storaged_info.cpp
parenta8776c0d616ed54d8569d114521caa6a1036c0f5 (diff)
downloadsystem_core-f2dd78bb2c9fe0e6482cf012e26664752fc64401.tar.gz
system_core-f2dd78bb2c9fe0e6482cf012e26664752fc64401.tar.bz2
system_core-f2dd78bb2c9fe0e6482cf012e26664752fc64401.zip
storaged: fix divide-by-zero when updating history
Bug: 75984894 Test: storaged unit tests on x86/64 platforms Change-Id: I869b3dc6e42aa71100a99a84344dbc30c319d280
Diffstat (limited to 'storaged/storaged_info.cpp')
-rw-r--r--storaged/storaged_info.cpp12
1 files changed, 8 insertions, 4 deletions
diff --git a/storaged/storaged_info.cpp b/storaged/storaged_info.cpp
index 055f3751f..5605f667c 100644
--- a/storaged/storaged_info.cpp
+++ b/storaged/storaged_info.cpp
@@ -157,11 +157,14 @@ void storage_info_t::update_perf_history(uint32_t bw,
return;
}
- recent_perf.erase(recent_perf.begin() + nr_samples,
- recent_perf.end());
+ if (nr_samples < recent_perf.size()) {
+ recent_perf.erase(recent_perf.begin() + nr_samples, recent_perf.end());
+ }
- uint32_t daily_avg_bw = accumulate(recent_perf.begin(),
- recent_perf.begin() + nr_samples, 0) / nr_samples;
+ uint32_t daily_avg_bw = 0;
+ if (!recent_perf.empty()) {
+ daily_avg_bw = accumulate(recent_perf.begin(), recent_perf.end(), 0) / recent_perf.size();
+ }
day_start_tp = tp - chrono::seconds(duration_cast<chrono::seconds>(
tp.time_since_epoch()).count() % DAY_TO_SEC);
@@ -176,6 +179,7 @@ void storage_info_t::update_perf_history(uint32_t bw,
return;
}
+ DCHECK(nr_days > 0);
uint32_t week_avg_bw = accumulate(daily_perf.begin(),
daily_perf.begin() + nr_days, 0) / nr_days;