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

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

Handle potentially null strings passed as ProtoLog parameter

Flag: NONE small bug fix
Test: atest com.android.internal.protolog.PerfettoProtoLogImplTest
Change-Id: Ieba2fbb281e8b030e47c557cb02b6001f8c1537c
parent f6f64620
Loading
Loading
Loading
Loading
+20 −4
Original line number Diff line number Diff line
@@ -91,6 +91,7 @@ import java.util.concurrent.locks.ReentrantLock;
 */
public class PerfettoProtoLogImpl implements IProtoLog {
    private static final String LOG_TAG = "ProtoLog";
    public static final String NULL_STRING = "null";
    private final AtomicInteger mTracingInstances = new AtomicInteger();

    private final ProtoLogDataSource mDataSource = new ProtoLogDataSource(
@@ -320,8 +321,14 @@ public class PerfettoProtoLogImpl implements IProtoLog {
            StringBuilder builder = new StringBuilder("UNKNOWN MESSAGE");
            if (args != null) {
                builder.append(" args = (");
                builder.append(String.join(", ", Arrays.stream(args).map(
                        Object::toString).toList()));
                builder.append(String.join(", ", Arrays.stream(args)
                        .map(it -> {
                            if (it == null) {
                                return "null";
                            } else {
                                return it.toString();
                            }
                        }).toList()));
                builder.append(")");
            }
            messageString = builder.toString();
@@ -412,8 +419,12 @@ public class PerfettoProtoLogImpl implements IProtoLog {
                for (Object o : args) {
                    int type = LogDataType.bitmaskToLogDataType(message.getMessageMask(), argIndex);
                    if (type == LogDataType.STRING) {
                        if (o == null) {
                            internStringArg(ctx, NULL_STRING);
                        } else {
                            internStringArg(ctx, o.toString());
                        }
                    }
                    argIndex++;
                }
            }
@@ -455,7 +466,12 @@ public class PerfettoProtoLogImpl implements IProtoLog {
                    try {
                        switch (type) {
                            case LogDataType.STRING:
                                final int internedStringId = internStringArg(ctx, o.toString());
                                final int internedStringId;
                                if (o == null) {
                                    internedStringId = internStringArg(ctx, NULL_STRING);
                                } else {
                                    internedStringId = internStringArg(ctx, o.toString());
                                }
                                os.write(STR_PARAM_IIDS, internedStringId);
                                needsIncrementalState = true;
                                break;
+23 −0
Original line number Diff line number Diff line
@@ -688,6 +688,29 @@ public class PerfettoProtoLogImplTest {
                .isFalse();
    }

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

        try {
            traceMonitor.start();

            mProtoLog.log(LogLevel.DEBUG, TestProtoLogGroup.TEST_GROUP,
                    "My test null string: %s", 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 test null string: null");
    }

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