diff --git a/logd/LogAudit.cpp b/logd/LogAudit.cpp index 7db17d1df..808d17a66 100644 --- a/logd/LogAudit.cpp +++ b/logd/LogAudit.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -153,15 +154,16 @@ int LogAudit::logPrint(const char *fmt, ...) { // log to events - size_t l = strlen(str); + size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD); size_t n = l + sizeof(android_log_event_string_t); bool notify = false; - android_log_event_string_t *event = static_cast(malloc(n)); - if (!event) { - rc = -ENOMEM; - } else { + { // begin scope for event buffer + uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; + + android_log_event_string_t *event + = reinterpret_cast(buffer); event->header.tag = htole32(AUDITD_LOG_TAG); event->type = EVENT_TYPE_STRING; event->length = htole32(l); @@ -170,11 +172,10 @@ int LogAudit::logPrint(const char *fmt, ...) { rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid, reinterpret_cast(event), (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX); - free(event); - if (rc >= 0) { notify = true; } + // end scope for event buffer } // log to main @@ -206,24 +207,28 @@ int LogAudit::logPrint(const char *fmt, ...) { l = strlen(comm) + 1; ecomm = ""; } - n = (estr - str) + strlen(ecomm) + l + 2; + size_t b = estr - str; + if (b > LOGGER_ENTRY_MAX_PAYLOAD) { + b = LOGGER_ENTRY_MAX_PAYLOAD; + } + size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b); + n = b + e + l + 2; + + { // begin scope for main buffer + char newstr[n]; - char *newstr = static_cast(malloc(n)); - if (!newstr) { - rc = -ENOMEM; - } else { *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN; strlcpy(newstr + 1, comm, l); - strncpy(newstr + 1 + l, str, estr - str); - strcpy(newstr + 1 + l + (estr - str), ecomm); + strncpy(newstr + 1 + l, str, b); + strncpy(newstr + 1 + l + b, ecomm, e); rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr, (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX); - free(newstr); if (rc >= 0) { notify = true; } + // end scope for main buffer } free(commfree); diff --git a/logd/LogBufferElement.cpp b/logd/LogBufferElement.cpp index 9fb143952..150ce2220 100644 --- a/logd/LogBufferElement.cpp +++ b/logd/LogBufferElement.cpp @@ -91,7 +91,8 @@ char *android::tidToName(pid_t tid) { size_t retval_len = strlen(retval); size_t name_len = strlen(name); // KISS: ToDo: Only checks prefix truncated, not suffix, or both - if ((retval_len < name_len) && !strcmp(retval, name + name_len - retval_len)) { + if ((retval_len < name_len) + && !fast(retval, name + name_len - retval_len)) { free(retval); retval = name; } else { @@ -123,14 +124,16 @@ size_t LogBufferElement::populateDroppedMessage(char *&buffer, commName = parent->pidToName(mPid); parent->unlock(); } - size_t len = name ? strlen(name) : 0; - if (len && commName && !strncmp(name, commName, len)) { - if (commName[len] == '\0') { - free(commName); - commName = NULL; - } else { - free(name); - name = NULL; + if (name && name[0] && commName && (name[0] == commName[0])) { + size_t len = strlen(name + 1); + if (!strncmp(name + 1, commName + 1, len)) { + if (commName[len + 1] == '\0') { + free(commName); + commName = NULL; + } else { + free(name); + name = NULL; + } } } if (name) { @@ -150,9 +153,9 @@ size_t LogBufferElement::populateDroppedMessage(char *&buffer, } } // identical to below to calculate the buffer size required - len = snprintf(NULL, 0, format_uid, mUid, name ? name : "", - commName ? commName : "", - mDropped, (mDropped > 1) ? "s" : ""); + size_t len = snprintf(NULL, 0, format_uid, mUid, name ? name : "", + commName ? commName : "", + mDropped, (mDropped > 1) ? "s" : ""); size_t hdrLen; if (mLogId == LOG_ID_EVENTS) { diff --git a/logd/LogBufferElement.h b/logd/LogBufferElement.h index 4877939c6..30e43c680 100644 --- a/logd/LogBufferElement.h +++ b/logd/LogBufferElement.h @@ -64,7 +64,7 @@ public: unsigned short getDropped(void) const { return mMsg ? 0 : mDropped; } unsigned short setDropped(unsigned short value) { if (mMsg) { - free(mMsg); + delete [] mMsg; mMsg = NULL; } return mDropped = value; diff --git a/logd/LogKlog.cpp b/logd/LogKlog.cpp index 242d7a0a4..d28161e5a 100644 --- a/logd/LogKlog.cpp +++ b/logd/LogKlog.cpp @@ -306,7 +306,7 @@ static const char *strnstr(const char *s, size_t len, const char *needle) { } --len; } while (*s++ != c); - } while (memcmp(s, needle, needleLen) != 0); + } while (fast(s, needle, needleLen)); s--; } return s; @@ -588,7 +588,7 @@ int LogKlog::log(const char *buf, size_t len) { const char *bt, *et, *cp; bt = p; - if (!strncmp(p, "[INFO]", 6)) { + if (!fast(p, "[INFO]", 6)) { // [