liblog: logprint, error return and truncated data

android_log_processBinaryLogBuffer and android_log_processLogBuffer
error return should have message and messageLen fields set to zero,
or to a valid-but-truncated buffer so that we can discern the
difference.  This will resolve an issue with reporting content from
an uninitialized field in liblogcat should caller add --debug flag.

To enhance the debugging capability, truncated string events because
of the logger limits are provided rather than dropping the field, but
still with an error return.

Some minor coding style issues resolved. Add required, or remove
extraneous spaces.  Use C-style comments only.

Test: gtest liblog-unit-tests
Bug: 27405083
Bug: 35326290
Change-Id: I4a7ddd7278fb1c582f921e1ba10e0765fadb791b
diff --git a/liblog/logprint.c b/liblog/logprint.c
index af52528..4421f83 100644
--- a/liblog/logprint.c
+++ b/liblog/logprint.c
@@ -20,9 +20,6 @@
 #define HAVE_STRSEP
 #endif
 
-//#ifndef __MINGW32__
-//#include <arpa/inet.h>
-//#endif
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
@@ -117,7 +114,7 @@
     c = tolower(c);
 
     if (c >= '0' && c <= '9') {
-        if (c >= ('0'+ANDROID_LOG_SILENT)) {
+        if (c >= ('0' + ANDROID_LOG_SILENT)) {
             pri = ANDROID_LOG_VERBOSE;
         } else {
             pri = (android_LogPriority)(c - '0');
@@ -397,7 +394,7 @@
     }
 
     if(filterExpression[tagNameLength] == ':') {
-        pri = filterCharToPri(filterExpression[tagNameLength+1]);
+        pri = filterCharToPri(filterExpression[tagNameLength + 1]);
 
         if (pri == ANDROID_LOG_UNKNOWN) {
             goto error;
@@ -521,6 +518,9 @@
         struct logger_entry *buf,
         AndroidLogEntry *entry)
 {
+    entry->message = NULL;
+    entry->messageLen = 0;
+
     entry->tv_sec = buf->sec;
     entry->tv_nsec = buf->nsec;
     entry->uid = -1;
@@ -621,7 +621,7 @@
 
     low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
     high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
-    return ((uint64_t) high << 32) | (uint64_t) low;
+    return ((uint64_t)high << 32) | (uint64_t)low;
 }
 
 static bool findChar(const char** cp, size_t* len, int c) {
@@ -829,7 +829,10 @@
             eventData += 4;
             eventDataLen -= 4;
 
-            if (eventDataLen < strLen) return -1;
+            if (eventDataLen < strLen) {
+                result = -1; /* mark truncated */
+                strLen = eventDataLen;
+            }
 
             if (cp && (strLen == 0)) {
                 /* reset the format if no content */
@@ -840,15 +843,18 @@
                 memcpy(outBuf, eventData, strLen);
                 outBuf += strLen;
                 outBufLen -= strLen;
-            } else if (outBufLen > 0) {
-                /* copy what we can */
-                memcpy(outBuf, eventData, outBufLen);
-                outBuf += outBufLen;
-                outBufLen -= outBufLen;
-                goto no_room;
+            } else {
+                if (outBufLen > 0) {
+                    /* copy what we can */
+                    memcpy(outBuf, eventData, outBufLen);
+                    outBuf += outBufLen;
+                    outBufLen -= outBufLen;
+                }
+                if (!result) result = 1; /* if not truncated, return no room */
             }
             eventData += strLen;
             eventDataLen -= strLen;
+            if (result != 0) goto bail;
             break;
         }
     case EVENT_TYPE_LIST:
@@ -991,13 +997,16 @@
 LIBLOG_ABI_PUBLIC int android_log_processBinaryLogBuffer(
         struct logger_entry *buf,
         AndroidLogEntry *entry,
-        const EventTagMap *map __unused, // only on !__ANDROID__
+        const EventTagMap *map __unused, /* only on !__ANDROID__ */
         char *messageBuf, int messageBufLen)
 {
     size_t inCount;
     uint32_t tagIndex;
     const unsigned char* eventData;
 
+    entry->message = NULL;
+    entry->messageLen = 0;
+
     entry->tv_sec = buf->sec;
     entry->tv_nsec = buf->nsec;
     entry->priority = ANDROID_LOG_INFO;
@@ -1009,7 +1018,7 @@
      * Pull the tag out, fill in some additional details based on incoming
      * buffer version (v3 adds lid, v4 adds uid).
      */
-    eventData = (const unsigned char*) buf->msg;
+    eventData = (const unsigned char*)buf->msg;
     struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf;
     if (buf2->hdr_size) {
         if ((buf2->hdr_size < sizeof(((struct log_msg *)NULL)->entry_v1)) ||
@@ -1122,7 +1131,7 @@
      */
     *outBuf = '\0';
     entry->messageLen = outBuf - messageBuf;
-    assert(entry->messageLen == (messageBufLen-1) - outRemaining);
+    assert(entry->messageLen == (messageBufLen - 1) - outRemaining);
 
     entry->message = messageBuf;
 
@@ -1217,7 +1226,7 @@
                 } else if (*message == '\b') {
                     strcpy(buf, "\\b");
                 } else if (*message == '\t') {
-                    strcpy(buf, "\t"); // Do not escape tabs
+                    strcpy(buf, "\t"); /* Do not escape tabs */
                 } else if (*message == '\v') {
                     strcpy(buf, "\\v");
                 } else if (*message == '\f') {
@@ -1574,7 +1583,7 @@
     nsec = entry->tv_nsec;
 #if __ANDROID__
     if (p_format->monotonic_output) {
-        // prevent convertMonotonic from being called if logd is monotonic
+        /* prevent convertMonotonic from being called if logd is monotonic */
         if (android_log_clockid() != CLOCK_MONOTONIC) {
             struct timespec time;
             convertMonotonic(&time, entry);
@@ -1648,7 +1657,7 @@
             } else
 #endif
             {
-                 // Not worth parsing package list, names all longer than 5
+                 /* Not worth parsing package list, names all longer than 5 */
                  snprintf(uid, sizeof(uid), "%5d:", entry->uid);
             }
         } else {
@@ -1758,7 +1767,7 @@
             if (*pm++ == '\n') numLines++;
         }
         /* plus one line for anything not newline-terminated at the end */
-        if (pm > entry->message && *(pm-1) != '\n') numLines++;
+        if (pm > entry->message && *(pm - 1) != '\n') numLines++;
     }
 
     /*