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

Commit c6ad69d1 authored by Josh Gao's avatar Josh Gao
Browse files

liblog: don't return 0xFFFFFFFF as an invalid log id.

There are a bunch of branches that check "id >= LOG_ID_MAX", but because
C++ hates you, this does a promotion to signed int despite the
fact that both sides of the comparison are the same enum with an
underlying type of unsigned int. (C++17 §7.6.3)

Return LOG_ID_MAX instead of a value that gets promoted to signed -1, to
avoid this.

Bug: http://b/129272512
Test: /data/nativetest64/logcat-unit-tests/logcat-unit-tests
Change-Id: I4b3ee662d76d5cc80d9a9625d17f7e5b5980de41
parent 8a5a918e
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -50,8 +50,9 @@ log_id_t android_name_to_log_id(const char* logName) {
  unsigned int ret;

  if (!logName) {
    return static_cast<log_id_t>(0xFFFFFFFF);
    return static_cast<log_id_t>(LOG_ID_MAX);
  }

  b = strrchr(logName, '/');
  if (!b) {
    b = logName;
@@ -65,5 +66,6 @@ log_id_t android_name_to_log_id(const char* logName) {
      return static_cast<log_id_t>(ret);
    }
  }
  return static_cast<log_id_t>(0xFFFFFFFF); /* should never happen */

  return static_cast<log_id_t>(LOG_ID_MAX);
}
+11 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@
#include <android-base/file.h>
#include <android-base/macros.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <gtest/gtest.h>
#include <log/event_tag_map.h>
#include <log/log.h>
@@ -1747,3 +1748,13 @@ TEST(logcat, help) {
    EXPECT_EQ(logcatHelpTextSize * 2, logcatLastHelpTextSize);
#endif
}

TEST(logcat, invalid_buffer) {
  FILE* fp = popen("logcat -b foo 2>&1", "r");
  ASSERT_NE(nullptr, fp);
  std::string output;
  ASSERT_TRUE(android::base::ReadFdToString(fileno(fp), &output));
  pclose(fp);

  ASSERT_TRUE(android::base::StartsWith(output, "unknown buffer foo\n"));
}