summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Andy Getzendanner <durandal@google.com>2022-12-19 07:43:33 -0800
committerGravatar Copybara-Service <copybara-worker@google.com>2022-12-19 07:45:02 -0800
commitb1f792822b8d25e2f954b32dbcee0ddb89631fa9 (patch)
tree3943ae69194ad51f885adb08d9c257248d8209aa
parent95505fbfbabc5240b623017d44093bb82f4912a9 (diff)
Fix a -Wsign-conversion and some -Wshorten-64-to-32.
PiperOrigin-RevId: 496397075 Change-Id: Ib44467cf5704b9147c7fd197e8343a666fada1c3
-rw-r--r--absl/log/BUILD.bazel1
-rw-r--r--absl/log/CMakeLists.txt1
-rw-r--r--absl/log/internal/proto.cc5
-rw-r--r--absl/log/internal/proto.h21
-rw-r--r--absl/log/log_format_test.cc11
5 files changed, 33 insertions, 6 deletions
diff --git a/absl/log/BUILD.bazel b/absl/log/BUILD.bazel
index 91639e64..e9e411ff 100644
--- a/absl/log/BUILD.bazel
+++ b/absl/log/BUILD.bazel
@@ -421,6 +421,7 @@ cc_test(
"//absl/log/internal:test_matchers",
"//absl/strings",
"//absl/strings:str_format",
+ "//absl/types:optional",
"@com_google_googletest//:gtest_main",
],
)
diff --git a/absl/log/CMakeLists.txt b/absl/log/CMakeLists.txt
index 4bc6f0d8..91bba002 100644
--- a/absl/log/CMakeLists.txt
+++ b/absl/log/CMakeLists.txt
@@ -859,6 +859,7 @@ absl_cc_test(
absl::check
absl::log
absl::log_internal_test_matchers
+ absl::optional
absl::scoped_mock_log
absl::str_format
absl::strings
diff --git a/absl/log/internal/proto.cc b/absl/log/internal/proto.cc
index 9a2b1a39..14b071a9 100644
--- a/absl/log/internal/proto.cc
+++ b/absl/log/internal/proto.cc
@@ -15,6 +15,7 @@
#include "absl/log/internal/proto.h"
#include <algorithm>
+#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
@@ -143,7 +144,9 @@ ABSL_MUST_USE_RESULT absl::Span<char> EncodeMessageStart(
void EncodeMessageLength(absl::Span<char> msg, const absl::Span<char> *buf) {
if (!msg.data()) return;
- const size_t length_size = msg.size();
+ assert(buf->data() >= msg.data());
+ if (buf->data() < msg.data()) return;
+ const uint64_t length_size = msg.size();
EncodeRawVarint(static_cast<uint64_t>(buf->data() - msg.data()) - length_size,
length_size, &msg);
}
diff --git a/absl/log/internal/proto.h b/absl/log/internal/proto.h
index 63c4e986..4458282b 100644
--- a/absl/log/internal/proto.h
+++ b/absl/log/internal/proto.h
@@ -70,6 +70,15 @@ namespace log_internal {
// Used for int32_t, int64_t, uint32_t, uint64_t, bool, and enum field types.
// Consumes up to kMaxVarintSize * 2 bytes (20).
bool EncodeVarint(uint64_t tag, uint64_t value, absl::Span<char> *buf);
+inline bool EncodeVarint(uint64_t tag, int64_t value, absl::Span<char> *buf) {
+ return EncodeVarint(tag, static_cast<uint64_t>(value), buf);
+}
+inline bool EncodeVarint(uint64_t tag, uint32_t value, absl::Span<char> *buf) {
+ return EncodeVarint(tag, static_cast<uint64_t>(value), buf);
+}
+inline bool EncodeVarint(uint64_t tag, int32_t value, absl::Span<char> *buf) {
+ return EncodeVarint(tag, static_cast<uint64_t>(value), buf);
+}
// Encodes the specified integer as a varint field using ZigZag encoding and
// returns true if it fits.
@@ -86,6 +95,15 @@ inline bool EncodeVarintZigZag(uint64_t tag, int64_t value,
// Used for fixed64 and sfixed64 field types.
// Consumes up to kMaxVarintSize + 8 bytes (18).
bool Encode64Bit(uint64_t tag, uint64_t value, absl::Span<char> *buf);
+inline bool Encode64Bit(uint64_t tag, int64_t value, absl::Span<char> *buf) {
+ return Encode64Bit(tag, static_cast<uint64_t>(value), buf);
+}
+inline bool Encode64Bit(uint64_t tag, uint32_t value, absl::Span<char> *buf) {
+ return Encode64Bit(tag, static_cast<uint64_t>(value), buf);
+}
+inline bool Encode64Bit(uint64_t tag, int32_t value, absl::Span<char> *buf) {
+ return Encode64Bit(tag, static_cast<uint64_t>(value), buf);
+}
// Encodes the specified double as a 64-bit field and returns true if it fits.
// Used for double field type.
@@ -98,6 +116,9 @@ inline bool EncodeDouble(uint64_t tag, double value, absl::Span<char> *buf) {
// Used for fixed32 and sfixed32 field types.
// Consumes up to kMaxVarintSize + 4 bytes (14).
bool Encode32Bit(uint64_t tag, uint32_t value, absl::Span<char> *buf);
+inline bool Encode32Bit(uint64_t tag, int32_t value, absl::Span<char> *buf) {
+ return Encode32Bit(tag, static_cast<uint32_t>(value), buf);
+}
// Encodes the specified float as a 32-bit field and returns true if it fits.
// Used for float field type.
diff --git a/absl/log/log_format_test.cc b/absl/log/log_format_test.cc
index 5ff6f159..69bdf8d8 100644
--- a/absl/log/log_format_test.cc
+++ b/absl/log/log_format_test.cc
@@ -36,6 +36,7 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
+#include "absl/types/optional.h"
namespace {
using ::absl::log_internal::AsString;
@@ -1702,18 +1703,18 @@ size_t MaxLogFieldLengthNoPrefix() {
class StringLengthExtractorSink : public absl::LogSink {
public:
void Send(const absl::LogEntry& entry) override {
- CHECK_EQ(size_, -1);
+ CHECK(!size_.has_value());
CHECK_EQ(entry.text_message().find_first_not_of('x'),
absl::string_view::npos);
- size_ = entry.text_message().size();
+ size_.emplace(entry.text_message().size());
}
size_t size() const {
- CHECK_GT(size_, 0);
- return size_;
+ CHECK(size_.has_value());
+ return *size_;
}
private:
- size_t size_ = -1;
+ absl::optional<size_t> size_;
} extractor_sink;
LOG(INFO).NoPrefix().ToSinkOnly(&extractor_sink)
<< std::string(2 * absl::log_internal::kLogMessageBufferSize, 'x');