Fix "unsafe narrowing" warnings in absl, 11/n.

Addresses failures with the following, in some files:
-Wshorten-64-to-32
-Wimplicit-int-conversion
-Wsign-compare
-Wsign-conversion
-Wtautological-unsigned-zero-compare

(This specific CL focuses on the logging facility.)

Bug: chromium:1292951
PiperOrigin-RevId: 479384741
Change-Id: Id450438ea3781ce25137366ca16757e810020ad4
pull/1290/head
Abseil Team 2 years ago committed by Copybara-Service
parent 10e7b4b72d
commit 8317b9a01c
  1. 2
      absl/log/internal/conditions.cc
  2. 23
      absl/log/internal/log_format.cc
  3. 17
      absl/log/internal/log_message.cc
  4. 7
      absl/log/log_entry_test.cc
  5. 2
      absl/log/log_format_test.cc

@ -37,7 +37,7 @@ uint32_t LossyIncrement(std::atomic<uint32_t>* counter) {
} // namespace
bool LogEveryNState::ShouldLog(int n) {
return n != 0 && (LossyIncrement(&counter_) % n) == 0;
return n > 0 && (LossyIncrement(&counter_) % static_cast<uint32_t>(n)) == 0;
}
bool LogFirstNState::ShouldLog(int n) {

@ -78,7 +78,7 @@ size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp,
absl::LogSeverityName(severity)[0], static_cast<int>(tv.tv_sec),
static_cast<int>(tv.tv_usec), static_cast<int>(tid));
if (snprintf_result >= 0) {
buf.remove_prefix(snprintf_result);
buf.remove_prefix(static_cast<size_t>(snprintf_result));
return static_cast<size_t>(snprintf_result);
}
return 0;
@ -87,26 +87,27 @@ size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp,
char* p = buf.data();
*p++ = absl::LogSeverityName(severity)[0];
const absl::TimeZone::CivilInfo ci = tz->At(timestamp);
absl::numbers_internal::PutTwoDigits(ci.cs.month(), p);
absl::numbers_internal::PutTwoDigits(static_cast<size_t>(ci.cs.month()), p);
p += 2;
absl::numbers_internal::PutTwoDigits(ci.cs.day(), p);
absl::numbers_internal::PutTwoDigits(static_cast<size_t>(ci.cs.day()), p);
p += 2;
*p++ = ' ';
absl::numbers_internal::PutTwoDigits(ci.cs.hour(), p);
absl::numbers_internal::PutTwoDigits(static_cast<size_t>(ci.cs.hour()), p);
p += 2;
*p++ = ':';
absl::numbers_internal::PutTwoDigits(ci.cs.minute(), p);
absl::numbers_internal::PutTwoDigits(static_cast<size_t>(ci.cs.minute()), p);
p += 2;
*p++ = ':';
absl::numbers_internal::PutTwoDigits(ci.cs.second(), p);
absl::numbers_internal::PutTwoDigits(static_cast<size_t>(ci.cs.second()), p);
p += 2;
*p++ = '.';
const int64_t usecs = absl::ToInt64Microseconds(ci.subsecond);
absl::numbers_internal::PutTwoDigits(usecs / 10000, p);
absl::numbers_internal::PutTwoDigits(static_cast<size_t>(usecs / 10000), p);
p += 2;
absl::numbers_internal::PutTwoDigits(usecs / 100 % 100, p);
absl::numbers_internal::PutTwoDigits(static_cast<size_t>(usecs / 100 % 100),
p);
p += 2;
absl::numbers_internal::PutTwoDigits(usecs % 100, p);
absl::numbers_internal::PutTwoDigits(static_cast<size_t>(usecs % 100), p);
p += 2;
*p++ = ' ';
constexpr bool unsigned_tid_t = !std::is_signed<log_internal::Tid>::value;
@ -118,7 +119,7 @@ size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp,
if ((unsigned_tid_t || tid > -100000) && tid < 1000000) *p++ = ' ';
p = absl::numbers_internal::FastIntToBuffer(tid, p);
*p++ = ' ';
const size_t bytes_formatted = p - buf.data();
const size_t bytes_formatted = static_cast<size_t>(p - buf.data());
buf.remove_prefix(bytes_formatted);
return bytes_formatted;
}
@ -146,7 +147,7 @@ size_t FormatLineNumber(int line, absl::Span<char>& buf) {
p = absl::numbers_internal::FastIntToBuffer(line, p);
*p++ = ']';
*p++ = ' ';
const size_t bytes_formatted = p - buf.data();
const size_t bytes_formatted = static_cast<size_t>(p - buf.data());
buf.remove_prefix(bytes_formatted);
return bytes_formatted;
}

@ -118,20 +118,23 @@ class LogEntryStreambuf final : public std::streambuf {
// If no data were ever streamed in, this is where we must write the prefix.
if (pbase() == nullptr) Initialize();
// Here we reclaim the two bytes we reserved.
size_t idx = pptr() - pbase();
ptrdiff_t idx = pptr() - pbase();
setp(buf_.data(), buf_.data() + buf_.size());
pbump(idx);
pbump(static_cast<int>(idx));
sputc('\n');
sputc('\0');
finalized_ = true;
return absl::Span<const char>(pbase(), pptr() - pbase());
return absl::Span<const char>(pbase(),
static_cast<size_t>(pptr() - pbase()));
}
size_t prefix_len() const { return prefix_len_; }
protected:
std::streamsize xsputn(const char* s, std::streamsize n) override {
if (n < 0) return 0;
if (pbase() == nullptr) Initialize();
return Append(absl::string_view(s, n));
return static_cast<std::streamsize>(
Append(absl::string_view(s, static_cast<size_t>(n))));
}
int overflow(int ch = EOF) override {
@ -154,14 +157,14 @@ class LogEntryStreambuf final : public std::streambuf {
prefix_len_ = log_internal::FormatLogPrefix(
entry_.log_severity(), entry_.timestamp(), entry_.tid(),
entry_.source_basename(), entry_.source_line(), remaining);
pbump(prefix_len_);
pbump(static_cast<int>(prefix_len_));
}
}
size_t Append(absl::string_view data) {
absl::Span<char> remaining(pptr(), epptr() - pptr());
absl::Span<char> remaining(pptr(), static_cast<size_t>(epptr() - pptr()));
const size_t written = AppendTruncated(data, &remaining);
pbump(written);
pbump(static_cast<int>(written));
return written;
}

@ -101,13 +101,14 @@ class LogEntryTestPeer {
entry_.source_basename(), entry_.source_line(), view)
: 0;
EXPECT_THAT(entry_.prefix_len_, Eq(view.data() - buf_.data()));
EXPECT_THAT(entry_.prefix_len_,
Eq(static_cast<size_t>(view.data() - buf_.data())));
AppendTruncated(text_message, view);
view = absl::Span<char>(view.data(), view.size() + 2);
view[0] = '\n';
view[1] = '\0';
view.remove_prefix(2);
buf_.resize(view.data() - buf_.data());
buf_.resize(static_cast<size_t>(view.data() - buf_.data()));
entry_.text_message_with_prefix_and_newline_and_nul_ = absl::MakeSpan(buf_);
}
LogEntryTestPeer(const LogEntryTestPeer&) = delete;
@ -124,7 +125,7 @@ class LogEntryTestPeer {
const size_t prefix_size = log_internal::FormatLogPrefix(
entry_.log_severity(), entry_.timestamp(), entry_.tid(),
entry_.source_basename(), entry_.source_line(), buf);
EXPECT_THAT(prefix_size, Eq(buf.data() - str.data()));
EXPECT_THAT(prefix_size, Eq(static_cast<size_t>(buf.data() - str.data())));
str.resize(prefix_size);
return str;
}

@ -108,7 +108,7 @@ TYPED_TEST(CharLogFormatTest, Printable) {
TYPED_TEST(CharLogFormatTest, Unprintable) {
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
const TypeParam value = 0xeeu;
constexpr auto value = static_cast<TypeParam>(0xeeu);
auto comparison_stream = ComparisonStream();
comparison_stream << value;

Loading…
Cancel
Save