From a820a0e587b1f6be7283588b744cd33ba5723bdf Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Wed, 26 Oct 2011 18:40:39 -0700 Subject: [PATCH] Gracefully handle truncated log messages. Bug: 5522726 Change-Id: I8637c7da854ec1ecb321632c45ee9bc2edc94a67 --- liblog/logprint.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/liblog/logprint.c b/liblog/logprint.c index daada5ad7..8366c94db 100644 --- a/liblog/logprint.c +++ b/liblog/logprint.c @@ -362,6 +362,9 @@ int android_log_processLogBuffer(struct logger_entry *buf, * starts at buf->msg+1 * msg * starts at buf->msg+1+len(tag)+1 + * + * The message may have been truncated by the kernel log driver. + * When that happens, we must null-terminate the message ourselves. */ if (buf->len < 3) { // An well-formed entry must consist of at least a priority @@ -370,21 +373,35 @@ int android_log_processLogBuffer(struct logger_entry *buf, return -1; } - int nullsFound = 0; + int msgStart = -1; + int msgEnd = -1; + int i; for (i = 1; i < buf->len; i++) { if (buf->msg[i] == '\0') { - nullsFound++; + if (msgStart == -1) { + msgStart = i + 1; + } else { + msgEnd = i; + break; + } } } - if (nullsFound != 2) { - fprintf(stderr, "+++ LOG: malformed log entry\n"); + + if (msgStart == -1) { + fprintf(stderr, "+++ LOG: malformed log message\n"); return -1; } + if (msgEnd == -1) { + // incoming message not null-terminated; force it + msgEnd = buf->len - 1; + buf->msg[msgEnd] = '\0'; + } + entry->priority = buf->msg[0]; entry->tag = buf->msg + 1; - entry->message = entry->tag + strlen(entry->tag) + 1; - entry->messageLen = strlen(entry->message); + entry->message = buf->msg + msgStart; + entry->messageLen = msgEnd - msgStart; return 0; }