Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 804e7d8c authored by Bernie Innocenti's avatar Bernie Innocenti
Browse files

Fix clang-tidy warnings in log_time.h

 - Zero initialize log_time instances by default
 - Disable implicit conversions by making constructors explicit
 - Explicitly initialize to EPOCH in most places
 - Change sniffTime() to avoid in-place modification of a log_time

I stopped here, but we could consider following up with a more invasive
change to make log_time instances immutable and perhaps also remove the
default constructor to force explicit initialization to EPOCH.

Test: atest libbase_test netd_unit_test
Change-Id: I67e716ef74adaaf40ab2c6e2e0dddb8d309bc7ca
parent ccb06c7b
Loading
Loading
Loading
Loading
+6 −10
Original line number Original line Diff line number Diff line
@@ -42,23 +42,19 @@ extern "C" {
 */
 */
struct log_time {
struct log_time {
 public:
 public:
  uint32_t tv_sec; /* good to Feb 5 2106 */
  uint32_t tv_sec = 0; /* good to Feb 5 2106 */
  uint32_t tv_nsec;
  uint32_t tv_nsec = 0;


  static const uint32_t tv_sec_max = 0xFFFFFFFFUL;
  static const uint32_t tv_sec_max = 0xFFFFFFFFUL;
  static const uint32_t tv_nsec_max = 999999999UL;
  static const uint32_t tv_nsec_max = 999999999UL;
  static const timespec EPOCH;


  log_time(const timespec& T)
  log_time() {}
      : tv_sec(static_cast<uint32_t>(T.tv_sec)),
  explicit log_time(const timespec& T)
        tv_nsec(static_cast<uint32_t>(T.tv_nsec)) {
      : tv_sec(static_cast<uint32_t>(T.tv_sec)), tv_nsec(static_cast<uint32_t>(T.tv_nsec)) {}
  }
  explicit log_time(uint32_t sec, uint32_t nsec = 0)
  explicit log_time(uint32_t sec, uint32_t nsec = 0)
      : tv_sec(sec), tv_nsec(nsec) {
      : tv_sec(sec), tv_nsec(nsec) {
  }
  }
#define __struct_log_time_private_defined
  static const timespec EPOCH;
  log_time() {
  }
#ifdef __linux__
#ifdef __linux__
  explicit log_time(clockid_t id) {
  explicit log_time(clockid_t id) {
    timespec T;
    timespec T;
+2 −2
Original line number Original line Diff line number Diff line
@@ -137,7 +137,7 @@ LIBLOG_ABI_PRIVATE char* log_time::strptime(const char* s, const char* format) {
LIBLOG_ABI_PRIVATE log_time log_time::operator-=(const timespec& T) {
LIBLOG_ABI_PRIVATE log_time log_time::operator-=(const timespec& T) {
  // No concept of negative time, clamp to EPOCH
  // No concept of negative time, clamp to EPOCH
  if (*this <= T) {
  if (*this <= T) {
    return *this = EPOCH;
    return *this = log_time(EPOCH);
  }
  }


  if (this->tv_nsec < (unsigned long int)T.tv_nsec) {
  if (this->tv_nsec < (unsigned long int)T.tv_nsec) {
@@ -165,7 +165,7 @@ LIBLOG_ABI_PRIVATE log_time log_time::operator+=(const timespec& T) {
LIBLOG_ABI_PRIVATE log_time log_time::operator-=(const log_time& T) {
LIBLOG_ABI_PRIVATE log_time log_time::operator-=(const log_time& T) {
  // No concept of negative time, clamp to EPOCH
  // No concept of negative time, clamp to EPOCH
  if (*this <= T) {
  if (*this <= T) {
    return *this = EPOCH;
    return *this = log_time(EPOCH);
  }
  }


  if (this->tv_nsec < T.tv_nsec) {
  if (this->tv_nsec < T.tv_nsec) {
+1 −1
Original line number Original line Diff line number Diff line
@@ -300,7 +300,7 @@ int LogAudit::logPrint(const char* fmt, ...) {
        return 0;
        return 0;
    }
    }


    log_time now;
    log_time now(log_time::EPOCH);


    static const char audit_str[] = " audit(";
    static const char audit_str[] = " audit(";
    char* timeptr = strstr(str, audit_str);
    char* timeptr = strstr(str, audit_str);
+1 −1
Original line number Original line Diff line number Diff line
@@ -399,7 +399,7 @@ void LogBuffer::log(LogBufferElement* elem) {
                        ((*it)->getLogId() != LOG_ID_KERNEL))) {
                        ((*it)->getLogId() != LOG_ID_KERNEL))) {
        mLogElements.push_back(elem);
        mLogElements.push_back(elem);
    } else {
    } else {
        log_time end = log_time::EPOCH;
        log_time end(log_time::EPOCH);
        bool end_set = false;
        bool end_set = false;
        bool end_always = false;
        bool end_always = false;


+17 −19
Original line number Original line Diff line number Diff line
@@ -197,9 +197,8 @@ char* android::log_strntok_r(char* s, ssize_t& len, char*& last,
    // NOTREACHED
    // NOTREACHED
}
}


log_time LogKlog::correction =
log_time LogKlog::correction = (log_time(CLOCK_REALTIME) < log_time(CLOCK_MONOTONIC))
    (log_time(CLOCK_REALTIME) < log_time(CLOCK_MONOTONIC))
                                       ? log_time(log_time::EPOCH)
        ? log_time::EPOCH
                                       : (log_time(CLOCK_REALTIME) - log_time(CLOCK_MONOTONIC));
                                       : (log_time(CLOCK_REALTIME) - log_time(CLOCK_MONOTONIC));


LogKlog::LogKlog(LogBuffer* buf, LogReader* reader, int fdWrite, int fdRead,
LogKlog::LogKlog(LogBuffer* buf, LogReader* reader, int fdWrite, int fdRead,
@@ -268,7 +267,7 @@ void LogKlog::calculateCorrection(const log_time& monotonic,
    static const char real_format[] = "%Y-%m-%d %H:%M:%S.%09q UTC";
    static const char real_format[] = "%Y-%m-%d %H:%M:%S.%09q UTC";
    if (len < (ssize_t)(strlen(real_format) + 5)) return;
    if (len < (ssize_t)(strlen(real_format) + 5)) return;


    log_time real;
    log_time real(log_time::EPOCH);
    const char* ep = real.strptime(real_string, real_format);
    const char* ep = real.strptime(real_string, real_format);
    if (!ep || (ep > &real_string[len]) || (real > log_time(CLOCK_REALTIME))) {
    if (!ep || (ep > &real_string[len]) || (real > log_time(CLOCK_REALTIME))) {
        return;
        return;
@@ -282,20 +281,20 @@ void LogKlog::calculateCorrection(const log_time& monotonic,
    tm.tm_isdst = -1;
    tm.tm_isdst = -1;
    localtime_r(&now, &tm);
    localtime_r(&now, &tm);
    if ((tm.tm_gmtoff < 0) && ((-tm.tm_gmtoff) > (long)real.tv_sec)) {
    if ((tm.tm_gmtoff < 0) && ((-tm.tm_gmtoff) > (long)real.tv_sec)) {
        real = log_time::EPOCH;
        real = log_time(log_time::EPOCH);
    } else {
    } else {
        real.tv_sec += tm.tm_gmtoff;
        real.tv_sec += tm.tm_gmtoff;
    }
    }
    if (monotonic > real) {
    if (monotonic > real) {
        correction = log_time::EPOCH;
        correction = log_time(log_time::EPOCH);
    } else {
    } else {
        correction = real - monotonic;
        correction = real - monotonic;
    }
    }
}
}


void LogKlog::sniffTime(log_time& now, const char*& buf, ssize_t len,
log_time LogKlog::sniffTime(const char*& buf, ssize_t len, bool reverse) {
                        bool reverse) {
    log_time now(log_time::EPOCH);
    if (len <= 0) return;
    if (len <= 0) return now;


    const char* cp = nullptr;
    const char* cp = nullptr;
    if ((len > 10) && (*buf == '[')) {
    if ((len > 10) && (*buf == '[')) {
@@ -310,7 +309,7 @@ void LogKlog::sniffTime(log_time& now, const char*& buf, ssize_t len,
        }
        }
        buf = cp;
        buf = cp;


        if (isMonotonic()) return;
        if (isMonotonic()) return now;


        const char* b;
        const char* b;
        if (((b = android::strnstr(cp, len, suspendStr))) &&
        if (((b = android::strnstr(cp, len, suspendStr))) &&
@@ -329,11 +328,11 @@ void LogKlog::sniffTime(log_time& now, const char*& buf, ssize_t len,
            //     trigger a check for ntp-induced or hardware clock drift.
            //     trigger a check for ntp-induced or hardware clock drift.
            log_time real(CLOCK_REALTIME);
            log_time real(CLOCK_REALTIME);
            log_time mono(CLOCK_MONOTONIC);
            log_time mono(CLOCK_MONOTONIC);
            correction = (real < mono) ? log_time::EPOCH : (real - mono);
            correction = (real < mono) ? log_time(log_time::EPOCH) : (real - mono);
        } else if (((b = android::strnstr(cp, len, suspendedStr))) &&
        } else if (((b = android::strnstr(cp, len, suspendedStr))) &&
                   (((b += strlen(suspendStr)) - cp) < len)) {
                   (((b += strlen(suspendStr)) - cp) < len)) {
            len -= b - cp;
            len -= b - cp;
            log_time real;
            log_time real(log_time::EPOCH);
            char* endp;
            char* endp;
            real.tv_sec = strtol(b, &endp, 10);
            real.tv_sec = strtol(b, &endp, 10);
            if ((*endp == '.') && ((endp - b) < len)) {
            if ((*endp == '.') && ((endp - b) < len)) {
@@ -345,7 +344,7 @@ void LogKlog::sniffTime(log_time& now, const char*& buf, ssize_t len,
                }
                }
                if (reverse) {
                if (reverse) {
                    if (real > correction) {
                    if (real > correction) {
                        correction = log_time::EPOCH;
                        correction = log_time(log_time::EPOCH);
                    } else {
                    } else {
                        correction -= real;
                        correction -= real;
                    }
                    }
@@ -363,6 +362,7 @@ void LogKlog::sniffTime(log_time& now, const char*& buf, ssize_t len,
            now = log_time(CLOCK_REALTIME);
            now = log_time(CLOCK_REALTIME);
        }
        }
    }
    }
    return now;
}
}


pid_t LogKlog::sniffPid(const char*& buf, ssize_t len) {
pid_t LogKlog::sniffPid(const char*& buf, ssize_t len) {
@@ -451,8 +451,7 @@ void LogKlog::synchronize(const char* buf, ssize_t len) {
    }
    }
    parseKernelPrio(cp, len - (cp - buf));
    parseKernelPrio(cp, len - (cp - buf));


    log_time now;
    log_time now = sniffTime(cp, len - (cp - buf), true);
    sniffTime(now, cp, len - (cp - buf), true);


    const char* suspended = android::strnstr(buf, len, suspendedStr);
    const char* suspended = android::strnstr(buf, len, suspendedStr);
    if (!suspended || (suspended > cp)) {
    if (!suspended || (suspended > cp)) {
@@ -468,7 +467,7 @@ void LogKlog::synchronize(const char* buf, ssize_t len) {
    }
    }
    parseKernelPrio(cp, len - (cp - buf));
    parseKernelPrio(cp, len - (cp - buf));


    sniffTime(now, cp, len - (cp - buf), true);
    sniffTime(cp, len - (cp - buf), true);
}
}


// Convert kernel log priority number into an Android Logger priority number
// Convert kernel log priority number into an Android Logger priority number
@@ -548,8 +547,7 @@ int LogKlog::log(const char* buf, ssize_t len) {
    const char* p = buf;
    const char* p = buf;
    int pri = parseKernelPrio(p, len);
    int pri = parseKernelPrio(p, len);


    log_time now;
    log_time now = sniffTime(p, len - (p - buf), false);
    sniffTime(now, p, len - (p - buf), false);


    // sniff for start marker
    // sniff for start marker
    const char* start = android::strnstr(p, len - (p - buf), klogdStr);
    const char* start = android::strnstr(p, len - (p - buf), klogdStr);
Loading