Merge "Fix numerical overflow in result in computeStorageCacheBytes"

This commit is contained in:
Neharika Jali
2022-01-06 11:23:19 +00:00
committed by Android (Google) Code Review
2 changed files with 6 additions and 5 deletions

View File

@@ -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;
}

View File

@@ -170,7 +170,7 @@ public class StorageManagerBaseTest extends InstrumentationTestCase {
when(mFile.getUsableSpace()).thenReturn(10000L);
when(mFile.getTotalSpace()).thenReturn(100000L);
long result = mSm.getStorageCacheBytes(mFile, 0);
assertThat(result).isEqualTo(4666L);
assertThat(result).isEqualTo(4667L);
}
/**