summaryrefslogtreecommitdiff
path: root/core/java
diff options
context:
space:
mode:
authorNeharika Jali <neharikajali@google.com>2022-01-05 12:52:14 +0000
committerNeharika Jali <neharikajali@google.com>2022-01-06 08:01:42 +0000
commit4bf22b5e9a3f595a7b5c1bb891078d0907507fb9 (patch)
treed7654d9511188017bb9bf3c9eeb962a662fd73e5 /core/java
parent1cd3bfd430a9c210f2ede49b70795f1d63704abd (diff)
Fix numerical overflow in result in computeStorageCacheBytes
Bug: 211756656 Test: atest atest frameworks/base/core/tests/coretests/src/android/os/storage/StorageManagerBaseTest#testGetStorageCacheBytesUnderModerateStorage Change-Id: I89fd358536708389d33e3077dcd0928332f5182f
Diffstat (limited to 'core/java')
-rw-r--r--core/java/android/os/storage/StorageManager.java9
1 files changed, 5 insertions, 4 deletions
diff --git a/core/java/android/os/storage/StorageManager.java b/core/java/android/os/storage/StorageManager.java
index 5e4057bd840d..29accb9b3187 100644
--- a/core/java/android/os/storage/StorageManager.java
+++ b/core/java/android/os/storage/StorageManager.java
@@ -1525,10 +1525,11 @@ public class StorageManager {
result = totalBytes * CACHE_RESERVE_PERCENT_LOW / 100;
} else {
// Else, linearly interpolate the amount of space to reserve
- result = ((CACHE_RESERVE_PERCENT_HIGH - CACHE_RESERVE_PERCENT_LOW)
- * (usableBytes - storageThresholdHighBytes) + CACHE_RESERVE_PERCENT_HIGH
- * (storageThresholdHighBytes - storageThresholdLowBytes)) * totalBytes
- / (100 * (storageThresholdHighBytes - storageThresholdLowBytes));
+ double slope = (CACHE_RESERVE_PERCENT_HIGH - CACHE_RESERVE_PERCENT_LOW) * totalBytes
+ / (100.0 * (storageThresholdHighBytes - storageThresholdLowBytes));
+ double intercept = totalBytes * CACHE_RESERVE_PERCENT_LOW / 100.0
+ - storageThresholdLowBytes * slope;
+ result = Math.round(slope * usableBytes + intercept);
}
return result;
}