Null pointer exception in FileRotator.java
Sometimes a null pointer exception is thrown when examining files
managed by the file rotator.
The logs from the test show that the Wifi state is changed a large
number of times. On every state change, a write operation is
initiated on the file system. This will eventually result in out
of memory and the call to mBasePath.list() in the maybeRotate(...)
method in FileRotator.java will return null so the iteration will
throw a NullPointerException.
Change-Id: I5d5980d9593bc9ec75281169ca27ee591809903f
diff --git a/core/java/com/android/internal/util/FileRotator.java b/core/java/com/android/internal/util/FileRotator.java
index 26235f1..71550be 100644
--- a/core/java/com/android/internal/util/FileRotator.java
+++ b/core/java/com/android/internal/util/FileRotator.java
@@ -336,7 +336,12 @@
final long deleteBefore = currentTimeMillis - mDeleteAgeMillis;
final FileInfo info = new FileInfo(mPrefix);
- for (String name : mBasePath.list()) {
+ String[] baseFiles = mBasePath.list();
+ if (baseFiles == null) {
+ return;
+ }
+
+ for (String name : baseFiles) {
if (!info.parse(name)) continue;
if (info.isActive()) {
diff --git a/core/tests/coretests/src/com/android/internal/util/FileRotatorTest.java b/core/tests/coretests/src/com/android/internal/util/FileRotatorTest.java
index 95f0e67..0e3c13a 100644
--- a/core/tests/coretests/src/com/android/internal/util/FileRotatorTest.java
+++ b/core/tests/coretests/src/com/android/internal/util/FileRotatorTest.java
@@ -43,7 +43,10 @@
import java.util.Arrays;
import java.util.Random;
+import junit.framework.Assert;
+
import libcore.io.IoUtils;
+import libcore.io.Libcore;
/**
* Tests for {@link FileRotator}.
@@ -367,6 +370,16 @@
assertReadAll(rotate, "bar");
}
+ public void testFileSystemInaccessible() throws Exception {
+ File inaccessibleDir = null;
+ String dirPath = getContext().getFilesDir() + File.separator + "inaccessible";
+ inaccessibleDir = new File(dirPath);
+ final FileRotator rotate = new FileRotator(inaccessibleDir, PREFIX, SECOND_IN_MILLIS, SECOND_IN_MILLIS);
+
+ // rotate should not throw on dir not mkdir-ed (or otherwise inaccessible)
+ rotate.maybeRotate(TEST_TIME);
+ }
+
private void touch(String... names) throws IOException {
for (String name : names) {
final OutputStream out = new FileOutputStream(new File(mBasePath, name));