From f85554e12ff1a75d4bc47ac602c9201acc43ecc6 Mon Sep 17 00:00:00 2001 From: James Hawkins Date: Thu, 28 Jul 2016 11:50:23 -0700 Subject: [PATCH] bootstat: Fix a potential unhandled exception for malformed input. In rare cases the hardware storage on the device may be hosed and return garbage. Use ParseInt which handles bad input instead of stoi. BUG: 29334139 Change-Id: I91aedc169df110bea8097775f73dda11def22311 (cherry picked from commit 4dded613b3aaee016adffd895edf3866426fc22a) --- bootstat/boot_event_record_store.cpp | 7 +++++-- bootstat/bootstat.cpp | 10 +++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/bootstat/boot_event_record_store.cpp b/bootstat/boot_event_record_store.cpp index ef4f68ec0..346eadae6 100644 --- a/bootstat/boot_event_record_store.cpp +++ b/bootstat/boot_event_record_store.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include "histogram_logger.h" #include "uptime_parser.h" @@ -57,8 +58,10 @@ bool ParseRecordEventTime(const std::string& path, int32_t* uptime) { // Ignore existing bootstat records (which do not contain file content). if (!content.empty()) { - int32_t value = std::stoi(content); - bootstat::LogHistogram("bootstat_mtime_matches_content", value == *uptime); + int32_t value; + if (android::base::ParseInt(content.c_str(), &value)) { + bootstat::LogHistogram("bootstat_mtime_matches_content", value == *uptime); + } } return true; diff --git a/bootstat/bootstat.cpp b/bootstat/bootstat.cpp index 08fc5aff1..26e0ffc53 100644 --- a/bootstat/bootstat.cpp +++ b/bootstat/bootstat.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include "boot_event_record_store.h" @@ -147,7 +148,10 @@ std::string CalculateBootCompletePrefix() { std::string boot_complete_prefix = "boot_complete"; std::string build_date_str = GetProperty("ro.build.date.utc"); - int32_t build_date = std::stoi(build_date_str); + int32_t build_date; + if (!android::base::ParseInt(build_date_str.c_str(), &build_date)) { + return std::string(); + } BootEventRecordStore boot_event_store; BootEventRecordStore::BootEventRecord record; @@ -171,6 +175,10 @@ void RecordBootComplete() { // ota_boot_complete. The latter signifies that the device is booting after // a system update. std::string boot_complete_prefix = CalculateBootCompletePrefix(); + if (boot_complete_prefix.empty()) { + // The system is hosed because the build date property could not be read. + return; + } // post_decrypt_time_elapsed is only logged on encrypted devices. if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {