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

Commit f807ee9b authored by Pablo Gamito's avatar Pablo Gamito
Browse files

Handle null object params

Sometimes the params passed may be null. We should handle these cases by providing default values of the null objects.

Flag: EXEMPT adding null checks
Test: atest com.android.internal.protolog.PerfettoProtoLogImplTest
Change-Id: Ifd24a63b24329c45dd53b27c451724749ee04300
parent b6026970
Loading
Loading
Loading
Loading
+15 −3
Original line number Diff line number Diff line
@@ -476,13 +476,25 @@ public class PerfettoProtoLogImpl implements IProtoLog {
                                needsIncrementalState = true;
                                break;
                            case LogDataType.LONG:
                                if (o == null) {
                                    longParams.add(0);
                                } else {
                                    longParams.add(((Number) o).longValue());
                                }
                                break;
                            case LogDataType.DOUBLE:
                                if (o == null) {
                                    doubleParams.add(0d);
                                } else {
                                    doubleParams.add(((Number) o).doubleValue());
                                }
                                break;
                            case LogDataType.BOOLEAN:
                                if (o == null) {
                                    booleanParams.add(false);
                                } else {
                                    booleanParams.add((boolean) o);
                                }
                                break;
                        }
                    } catch (ClassCastException ex) {
+23 −0
Original line number Diff line number Diff line
@@ -711,6 +711,29 @@ public class PerfettoProtoLogImplTest {
                .isEqualTo("My test null string: null");
    }

    @Test
    public void supportNullParams() throws IOException {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true)
                        .build();

        try {
            traceMonitor.start();

            mProtoLog.log(LogLevel.DEBUG, TestProtoLogGroup.TEST_GROUP,
                    "My null args: %d, %f, %b", null, null, null);
        } finally {
            traceMonitor.stop(mWriter);
        }

        final ResultReader reader = new ResultReader(mWriter.write(), mTraceConfig);
        final ProtoLogTrace protolog = reader.readProtoLogTrace();

        Truth.assertThat(protolog.messages).hasSize(1);
        Truth.assertThat(protolog.messages.get(0).getMessage())
                .isEqualTo("My null args: 0, 0, false");
    }

    private enum TestProtoLogGroup implements IProtoLogGroup {
        TEST_GROUP(true, true, false, "TEST_TAG");