summaryrefslogtreecommitdiffstats
path: root/bootstat
diff options
context:
space:
mode:
authorMark Salyzyn <salyzyn@google.com>2017-09-20 08:37:46 -0700
committerMark Salyzyn <salyzyn@google.com>2017-10-26 14:17:14 -0700
commit747c0e6216f2d805ac0ac91c3d5296e11124165b (patch)
treeefe2244a76d613995a99dec3475c5d798aa4c6d8 /bootstat
parentdafced93a56352b79c74b016eba437b28bbbe11e (diff)
downloadsystem_core-747c0e6216f2d805ac0ac91c3d5296e11124165b.tar.gz
system_core-747c0e6216f2d805ac0ac91c3d5296e11124165b.tar.bz2
system_core-747c0e6216f2d805ac0ac91c3d5296e11124165b.zip
bootstat: better validation of battery level (shutdown,battery)
Replace simple strtoull with loop that ensures no leading zeros. Restrict size of value buffer being checked as allocation was going to end of retrieved buffer, which can cause unnecessary memory pressure during boot. Test: system/core/bootstat/boot_reason_test.sh Bug: 63736262 Change-Id: Ifdc1d4fd3a73794c001577024ce7cbfde9c25028
Diffstat (limited to 'bootstat')
-rw-r--r--bootstat/bootstat.cpp24
1 files changed, 18 insertions, 6 deletions
diff --git a/bootstat/bootstat.cpp b/bootstat/bootstat.cpp
index 07d410c91..cdb4b9b3b 100644
--- a/bootstat/bootstat.cpp
+++ b/bootstat/bootstat.cpp
@@ -508,10 +508,16 @@ std::string BootReasonStrToReason(const std::string& boot_reason) {
size_t pos = content.rfind(battery); // last one
std::string digits;
if (pos != std::string::npos) {
- digits = content.substr(pos + strlen(battery));
+ digits = content.substr(pos + strlen(battery), strlen("100 "));
+ }
+ const char* endptr = digits.c_str();
+ unsigned level = 0;
+ while (::isdigit(*endptr)) {
+ level *= 10;
+ level += *endptr++ - '0';
+ // make sure no leading zeros, except zero itself, and range check.
+ if ((level == 0) || (level > 100)) break;
}
- char* endptr = NULL;
- unsigned long long level = strtoull(digits.c_str(), &endptr, 10);
if ((level <= 100) && (endptr != digits.c_str()) && (*endptr == ' ')) {
LOG(INFO) << "Battery level at shutdown " << level << "%";
if (level <= battery_dead_threshold) {
@@ -552,10 +558,16 @@ std::string BootReasonStrToReason(const std::string& boot_reason) {
pos = content.find(match); // The first one it finds.
if (pos != std::string::npos) {
- digits = content.substr(pos + strlen(match));
+ digits = content.substr(pos + strlen(match), strlen("100 "));
+ }
+ endptr = digits.c_str();
+ level = 0;
+ while (::isdigit(*endptr)) {
+ level *= 10;
+ level += *endptr++ - '0';
+ // make sure no leading zeros, except zero itself, and range check.
+ if ((level == 0) || (level > 100)) break;
}
- endptr = NULL;
- level = strtoull(digits.c_str(), &endptr, 10);
if ((level <= 100) && (endptr != digits.c_str()) && (*endptr == ' ')) {
LOG(INFO) << "Battery level at startup " << level << "%";
if (level <= battery_dead_threshold) {