Avoid integer underflow involving maxFreqCount.
If a device does not have any policy directories under
/sys/devices/system/cpu/cpufreq this would previously lead to the
cputimeinstate subsystem being initialized with an empty set of policy
frequencies. This would lead to integer underflows in various loops
that enumerate the frequencies when subtracting 1 from a maxFreqCount
variable calculated as 0, resulting in us spending a significant amount
of time in these loops, likely leading to an ANR in system_server
since at least the loop in clearUidTimes is executed while holding the
BatteryStatsImpl lock. Fix the problem by skipping the initialization
of cputimeinstate if there are no policy directories.
Bug: 142352330
Bug: 178231152
Change-Id: I2ec1e8de0fe2a40ed100c8f14e6ca3f6d6285b82
diff --git a/libs/cputimeinstate/cputimeinstate.cpp b/libs/cputimeinstate/cputimeinstate.cpp
index 4209dc5..22103d5 100644
--- a/libs/cputimeinstate/cputimeinstate.cpp
+++ b/libs/cputimeinstate/cputimeinstate.cpp
@@ -98,7 +98,7 @@
struct dirent **dirlist;
const char basepath[] = "/sys/devices/system/cpu/cpufreq";
int ret = scandir(basepath, &dirlist, isPolicyFile, comparePolicyFiles);
- if (ret == -1) return false;
+ if (ret == -1 || ret == 0) return false;
gNPolicies = ret;
std::vector<std::string> policyFileNames;