Gracefully handle truncated log messages.

Bug: 5522726
Change-Id: I8637c7da854ec1ecb321632c45ee9bc2edc94a67
diff --git a/liblog/logprint.c b/liblog/logprint.c
index daada5a..8366c94 100644
--- a/liblog/logprint.c
+++ b/liblog/logprint.c
@@ -362,6 +362,9 @@
      *   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 @@
         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;
 }