Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4bf22b5e authored by Neharika Jali's avatar Neharika Jali
Browse files

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
parent 1cd3bfd4
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -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;
    }
+1 −1
Original line number Diff line number Diff line
@@ -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);
    }

    /**