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
This commit is contained in:
parent
dafced93a5
commit
747c0e6216
1 changed files with 18 additions and 6 deletions
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue