From b28c9d6b2d3b52d7c4ac1ad8c18591be99e92772 Mon Sep 17 00:00:00 2001 From: Daniel Nishi Date: Thu, 30 Mar 2017 15:46:01 -0700 Subject: Fix crash when measuring storage. If we are measuring storage and a volume is removed at the same time, we can enter a state where we previously verified the volume existed, but it no longer does. This causes an NPE. By adding in a null check, we can avoid this crash. Change-Id: Ib8dbf05102a122bdf4bb6063374e993a1de68425 Fixes: 36689190 Test: None --- .../android/settingslib/deviceinfo/StorageMeasurement.java | 11 +++++++++-- .../java/com/android/server/usage/StorageStatsService.java | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/deviceinfo/StorageMeasurement.java b/packages/SettingsLib/src/com/android/settingslib/deviceinfo/StorageMeasurement.java index 953dda29d7c4..60e10a125d0b 100644 --- a/packages/SettingsLib/src/com/android/settingslib/deviceinfo/StorageMeasurement.java +++ b/packages/SettingsLib/src/com/android/settingslib/deviceinfo/StorageMeasurement.java @@ -151,8 +151,15 @@ public class StorageMeasurement { final MeasurementDetails details = new MeasurementDetails(); if (mVolume == null) return details; - details.totalSize = mStats.getTotalBytes(mVolume.fsUuid); - details.availSize = mStats.getFreeBytes(mVolume.fsUuid); + try { + details.totalSize = mStats.getTotalBytes(mVolume.fsUuid); + details.availSize = mStats.getFreeBytes(mVolume.fsUuid); + } catch (IllegalStateException e) { + // The storage volume became null while we were measuring it. + Log.w(TAG, e); + return details; + } + final long finishTotal = SystemClock.elapsedRealtime(); Log.d(TAG, "Measured total storage in " + (finishTotal - start) + "ms"); diff --git a/services/usage/java/com/android/server/usage/StorageStatsService.java b/services/usage/java/com/android/server/usage/StorageStatsService.java index 3f39e4f3cf4b..5ad7f8042c01 100644 --- a/services/usage/java/com/android/server/usage/StorageStatsService.java +++ b/services/usage/java/com/android/server/usage/StorageStatsService.java @@ -162,6 +162,9 @@ public class StorageStatsService extends IStorageStatsManager.Stub { return FileUtils.roundStorageSize(mStorage.getPrimaryStorageSize()); } else { final VolumeInfo vol = mStorage.findVolumeByUuid(volumeUuid); + if (vol == null) { + throw new IllegalStateException("Volume was unexpected null"); + } return FileUtils.roundStorageSize(vol.disk.size); } } @@ -185,6 +188,9 @@ public class StorageStatsService extends IStorageStatsManager.Stub { return Environment.getDataDirectory().getUsableSpace() + cacheBytes; } else { final VolumeInfo vol = mStorage.findVolumeByUuid(volumeUuid); + if (vol == null) { + throw new IllegalStateException("Volume was unexpected null"); + } return vol.getPath().getUsableSpace() + cacheBytes; } } -- cgit v1.2.3-59-g8ed1b