Merge "Gracefully handle truncated log messages." into ics-mr1

This commit is contained in:
Jeff Sharkey 2011-10-28 11:56:17 -07:00 committed by Android (Google) Code Review
commit 70212a404a
1 changed files with 23 additions and 6 deletions

View File

@ -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;
}