From 8317b9a01cbc32594ad4bf971709c97cb13ec921 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 6 Oct 2022 12:31:33 -0700 Subject: [PATCH] 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 --- absl/log/internal/conditions.cc | 2 +- absl/log/internal/log_format.cc | 23 ++++++++++++----------- absl/log/internal/log_message.cc | 17 ++++++++++------- absl/log/log_entry_test.cc | 7 ++++--- absl/log/log_format_test.cc | 2 +- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/absl/log/internal/conditions.cc b/absl/log/internal/conditions.cc index 70f2acef..a9f4966f 100644 --- a/absl/log/internal/conditions.cc +++ b/absl/log/internal/conditions.cc @@ -37,7 +37,7 @@ uint32_t LossyIncrement(std::atomic* counter) { } // namespace bool LogEveryNState::ShouldLog(int n) { - return n != 0 && (LossyIncrement(&counter_) % n) == 0; + return n > 0 && (LossyIncrement(&counter_) % static_cast(n)) == 0; } bool LogFirstNState::ShouldLog(int n) { diff --git a/absl/log/internal/log_format.cc b/absl/log/internal/log_format.cc index b10a656b..5d40d253 100644 --- a/absl/log/internal/log_format.cc +++ b/absl/log/internal/log_format.cc @@ -78,7 +78,7 @@ size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp, absl::LogSeverityName(severity)[0], static_cast(tv.tv_sec), static_cast(tv.tv_usec), static_cast(tid)); if (snprintf_result >= 0) { - buf.remove_prefix(snprintf_result); + buf.remove_prefix(static_cast(snprintf_result)); return static_cast(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(ci.cs.month()), p); p += 2; - absl::numbers_internal::PutTwoDigits(ci.cs.day(), p); + absl::numbers_internal::PutTwoDigits(static_cast(ci.cs.day()), p); p += 2; *p++ = ' '; - absl::numbers_internal::PutTwoDigits(ci.cs.hour(), p); + absl::numbers_internal::PutTwoDigits(static_cast(ci.cs.hour()), p); p += 2; *p++ = ':'; - absl::numbers_internal::PutTwoDigits(ci.cs.minute(), p); + absl::numbers_internal::PutTwoDigits(static_cast(ci.cs.minute()), p); p += 2; *p++ = ':'; - absl::numbers_internal::PutTwoDigits(ci.cs.second(), p); + absl::numbers_internal::PutTwoDigits(static_cast(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(usecs / 10000), p); p += 2; - absl::numbers_internal::PutTwoDigits(usecs / 100 % 100, p); + absl::numbers_internal::PutTwoDigits(static_cast(usecs / 100 % 100), + p); p += 2; - absl::numbers_internal::PutTwoDigits(usecs % 100, p); + absl::numbers_internal::PutTwoDigits(static_cast(usecs % 100), p); p += 2; *p++ = ' '; constexpr bool unsigned_tid_t = !std::is_signed::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(p - buf.data()); buf.remove_prefix(bytes_formatted); return bytes_formatted; } @@ -146,7 +147,7 @@ size_t FormatLineNumber(int line, absl::Span& 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(p - buf.data()); buf.remove_prefix(bytes_formatted); return bytes_formatted; } diff --git a/absl/log/internal/log_message.cc b/absl/log/internal/log_message.cc index 9ef0c29e..82833af0 100644 --- a/absl/log/internal/log_message.cc +++ b/absl/log/internal/log_message.cc @@ -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(idx)); sputc('\n'); sputc('\0'); finalized_ = true; - return absl::Span(pbase(), pptr() - pbase()); + return absl::Span(pbase(), + static_cast(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( + Append(absl::string_view(s, static_cast(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(prefix_len_)); } } size_t Append(absl::string_view data) { - absl::Span remaining(pptr(), epptr() - pptr()); + absl::Span remaining(pptr(), static_cast(epptr() - pptr())); const size_t written = AppendTruncated(data, &remaining); - pbump(written); + pbump(static_cast(written)); return written; } diff --git a/absl/log/log_entry_test.cc b/absl/log/log_entry_test.cc index b19794e4..8d0afb3c 100644 --- a/absl/log/log_entry_test.cc +++ b/absl/log/log_entry_test.cc @@ -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(view.data() - buf_.data()))); AppendTruncated(text_message, view); view = absl::Span(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(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(buf.data() - str.data()))); str.resize(prefix_size); return str; } diff --git a/absl/log/log_format_test.cc b/absl/log/log_format_test.cc index 3fdb358a..c629fce7 100644 --- a/absl/log/log_format_test.cc +++ b/absl/log/log_format_test.cc @@ -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(0xeeu); auto comparison_stream = ComparisonStream(); comparison_stream << value;