Skip to content

Commit cf3a86f

Browse files
mnkassisjoeyhuab
authored andcommitted
Avoid NPE in CriticalEventLog.saveLogToFileNow()
A malformed event can cause an NPE in CriticalEventLog.saveLogToFileNow() when CriticalEventLogStorageProto.toByteArray() calls computeSerializedSize() on an event with null fields. This leads to a crash system_server and will force a device reboot. Move the toByteArray() call into the try-with-resources block and broaden the catch from IOException to Exception so that any exception during serialization or writing is caught and logged rather than crashing the system_server. Bug: None Test: atest CriticalEventLogTest Change-Id: I57190fc3245f9f03950f0cf7597fa3d4d2a5b550
1 parent bdb5f7d commit cf3a86f

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

services/core/java/com/android/server/criticalevents/CriticalEventLog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,10 @@ protected void saveLogToFileNow() {
397397
CriticalEventLogStorageProto logProto = new CriticalEventLogStorageProto();
398398
logProto.events = mEvents.toArray();
399399

400-
final byte[] bytes = CriticalEventLogStorageProto.toByteArray(logProto);
401400
try (FileOutputStream stream = new FileOutputStream(mLogFile, false)) {
401+
final byte[] bytes = CriticalEventLogStorageProto.toByteArray(logProto);
402402
stream.write(bytes);
403-
} catch (IOException e) {
403+
} catch (Exception e) {
404404
Slog.e(TAG, "Error saving log to disk.", e);
405405
}
406406
}

services/tests/servicestests/src/com/android/server/criticalevents/CriticalEventLogTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,22 @@ public void saveDelayMs() {
576576
assertThat(mCriticalEventLog.saveDelayMs()).isEqualTo(2000L);
577577
}
578578

579+
@Test
580+
public void saveLogToFileNow_withMalformedEvent_doesNotThrow() {
581+
CriticalEventProto malformedEvent = new CriticalEventProto();
582+
malformedEvent.timestampMs = START_TIME_MS + 1000;
583+
// Set a JavaCrash with null exceptionClass - this was causing NPE in computeSerializedSize()
584+
JavaCrash javaCrash = new JavaCrash();
585+
javaCrash.exceptionClass = null;
586+
javaCrash.process = null;
587+
malformedEvent.setJavaCrash(javaCrash);
588+
589+
mCriticalEventLog.appendAndSave(malformedEvent);
590+
591+
mCriticalEventLog.saveLogToFileNow();
592+
// should not throw
593+
}
594+
579595
@Test
580596
public void simulateReboot_saveAndLoadCycle() {
581597
TestableCriticalEventLog log1 = setLogInstance();

0 commit comments

Comments
 (0)