From 11e7e2e2969d19570c6206f368e8165bd1a8ac77 Mon Sep 17 00:00:00 2001 From: Isaac Peka <7493006+isaac-peka@users.noreply.github.com> Date: Fri, 16 Feb 2024 11:52:56 +0000 Subject: [PATCH] Fix builds for older version of libprotobuf --- .gitignore | 1 + examples/libfuzzer/libfuzzer_bin_example.cc | 2 +- examples/libfuzzer/libfuzzer_example.cc | 2 +- src/field_instance.h | 16 ++++++++++++++++ src/mutator.cc | 2 +- src/mutator_test.cc | 4 ++-- 6 files changed, 22 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 567609b..6f31401 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build/ +.vscode/ diff --git a/examples/libfuzzer/libfuzzer_bin_example.cc b/examples/libfuzzer/libfuzzer_bin_example.cc index 0734caa..cd31aad 100644 --- a/examples/libfuzzer/libfuzzer_bin_example.cc +++ b/examples/libfuzzer/libfuzzer_bin_example.cc @@ -55,7 +55,7 @@ DEFINE_BINARY_PROTO_FUZZER(const libfuzzer_example::Msg& message) { !std::isnan(message.optional_float()) && std::fabs(message.optional_float()) > 1000 && message.any().UnpackTo(&file) && !file.name().empty()) { - std::cerr << absl::StrCat(message) << "\n"; + std::cerr << message.DebugString() << "\n"; abort(); } } diff --git a/examples/libfuzzer/libfuzzer_example.cc b/examples/libfuzzer/libfuzzer_example.cc index 37c4e6b..2e6e887 100644 --- a/examples/libfuzzer/libfuzzer_example.cc +++ b/examples/libfuzzer/libfuzzer_example.cc @@ -55,7 +55,7 @@ DEFINE_PROTO_FUZZER(const libfuzzer_example::Msg& message) { !std::isnan(message.optional_float()) && std::fabs(message.optional_float()) > 1000 && message.any().UnpackTo(&file) && !file.name().empty()) { - std::cerr << absl::StrCat(message) << "\n"; + std::cerr << message.DebugString() << "\n"; abort(); } } diff --git a/src/field_instance.h b/src/field_instance.h index d5ecd84..d25a513 100644 --- a/src/field_instance.h +++ b/src/field_instance.h @@ -190,6 +190,21 @@ class ConstFieldInstance { return descriptor_->message_type(); } +#if PROTOBUF_VERSION < 3019006 + bool EnforceUtf8() const { + return descriptor_->type() == protobuf::FieldDescriptor::TYPE_STRING && + descriptor()->file()->syntax() == + protobuf::FileDescriptor::SYNTAX_PROTO3; + } + + const protobuf::FieldDescriptor* descriptor() const { return descriptor_; } + + std::string DebugString() const { + std::string s = descriptor_->DebugString(); + if (is_repeated()) s += "[" + std::to_string(index_) + "]"; + return s + " of\n" + message_->DebugString(); + } +#else bool EnforceUtf8() const { return descriptor_->requires_utf8_validation(); } const protobuf::FieldDescriptor* descriptor() const { return descriptor_; } @@ -199,6 +214,7 @@ class ConstFieldInstance { if (is_repeated()) s += absl::StrCat("[", index_, "]"); return s + " of\n" + absl::StrCat(*message_); } +#endif protected: bool is_repeated() const { return descriptor_->is_repeated(); } diff --git a/src/mutator.cc b/src/mutator.cc index 6977ea7..eaa7571 100644 --- a/src/mutator.cc +++ b/src/mutator.cc @@ -801,7 +801,7 @@ std::string Mutator::MutateUtf8String(const std::string& value, bool Mutator::IsInitialized(const Message& message) const { if (!keep_initialized_ || message.IsInitialized()) return true; - std::cerr << "Uninitialized: " << absl::StrCat(message) << "\n"; + std::cerr << "Uninitialized: " << message.DebugString() << "\n"; return false; } diff --git a/src/mutator_test.cc b/src/mutator_test.cc index 32f4e7d..17c5542 100644 --- a/src/mutator_test.cc +++ b/src/mutator_test.cc @@ -410,8 +410,8 @@ bool Mutate(const protobuf::Message& from, const protobuf::Message& to, } ADD_FAILURE() << "Failed to get from:\n" - << absl::StrCat(from) << "\nto:\n" - << absl::StrCat(to); + << from.DebugString() << "\nto:\n" + << to.DebugString(); return false; }