From 083c53462aefaee142ddfb40ba0ed8d40e1ecda4 Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Mon, 21 Mar 2016 09:45:34 -0700 Subject: [PATCH] liblog: logprint: deal with malformed log messages Try to print as much content as possible should the application logging only submit content as part of a tag with an empty message. We search for the first non-printable ascii character in the tag, in order to split it up for printing. Applications (such as com.yahoo.mobile.client.android.weather) that malform their log messages will no longer be punished by truncating the content, but this should never be considered advocacy for their bad behavior. Bug: 27585978 Change-Id: Idb0680e8d6a6ad2bef5150661905acccb5b70afb --- liblog/logprint.c | 14 ++++++++++++-- liblog/tests/liblog_test.cpp | 4 +++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/liblog/logprint.c b/liblog/logprint.c index 02df8ddd6..7f02b4690 100644 --- a/liblog/logprint.c +++ b/liblog/logprint.c @@ -512,8 +512,18 @@ LIBLOG_ABI_PUBLIC int android_log_processLogBuffer( } if (msgStart == -1) { - fprintf(stderr, "+++ LOG: malformed log message\n"); - return -1; + /* +++ LOG: malformed log message, DYB */ + for (i = 1; i < buf->len; i++) { + /* odd characters in tag? */ + if ((msg[i] <= ' ') || (msg[i] == ':') || (msg[i] >= 0x7f)) { + msg[i] = '\0'; + msgStart = i + 1; + break; + } + } + if (msgStart == -1) { + msgStart = buf->len - 1; /* All tag, no message, print truncates */ + } } if (msgEnd == -1) { /* incoming message not null-terminated; force it */ diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp index 2767f73b0..ca2cda277 100644 --- a/liblog/tests/liblog_test.cpp +++ b/liblog/tests/liblog_test.cpp @@ -1062,8 +1062,10 @@ TEST(liblog, __android_log_buf_print__maxtag) { fflush(stderr); int printLogLine = android_log_printLogLine(logformat, fileno(stderr), &entry); + // Legacy tag truncation EXPECT_LE(128, printLogLine); - EXPECT_GT(LOGGER_ENTRY_MAX_PAYLOAD, printLogLine); + // Measured maximum if we try to print part of the tag as message + EXPECT_GT(LOGGER_ENTRY_MAX_PAYLOAD * 13 / 8, printLogLine); } android_log_format_free(logformat); }