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

Commit 572c5df5 authored by Chris Wren's avatar Chris Wren
Browse files

guard against nulls in LogMaker

Bug: 36909905
Test: runtest --path frameworks/base/core/tests/coretests/src/android/metrics
Change-Id: Ibd12bf12f9b1ed5fbc926ec04bbfa429ff98f7d2
parent 94ac6ae6
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -54,7 +54,11 @@ public class LogMaker {

    /* Deserialize from the eventlog */
    public LogMaker(Object[] items) {
        if (items != null) {
            deserialize(items);
        } else {
            setCategory(MetricsEvent.VIEW_UNKNOWN);
        }
    }

    /** @param category to replace the existing setting. */
@@ -373,13 +377,13 @@ public class LogMaker {
     */
    public void deserialize(Object[] items) {
        int i = 0;
        while (i < items.length) {
        while (items != null && i < items.length) {
            Object key = items[i++];
            Object value = i < items.length ? items[i++] : null;
            if (key instanceof Integer) {
                entries.put((Integer) key, value);
            } else {
                Log.i(TAG, "Invalid key " + key.toString());
                Log.i(TAG, "Invalid key " + (key == null ? "null" : key.toString()));
            }
        }
    }
+28 −0
Original line number Diff line number Diff line
@@ -263,4 +263,32 @@ public class LogMakerTest extends TestCase {
        assertFalse(a.isSubsetOf(b));
        assertFalse(b.isSubsetOf(a));
    }

    public void testConstructFromNull() {
        new LogMaker(null);
        // no promises, just don't throw
    }

    public void testConstructFromNullKey() {
        Object[] items = new Object[2];
        items[0] = null;
        items[1] = "foo";
        new LogMaker(items);
        // no promises, just don't throw
    }

    public void testConstructFromNullField() {
        Object[] items = new Object[2];
        items[0] = 10;
        items[1] = null;
        new LogMaker(items);
        // no promises, just don't throw
    }

    public void testConstructFromTruncatedArray() {
        Object[] items = new Object[1];
        items[0] = 10;
        new LogMaker(items);
        // no promises, just don't throw
    }
}