Downgrade type of `string` extension fields to `bytes` if UTF8 validation is not validated.

PiperOrigin-RevId: 712107026
pull/19859/head
Protobuf Team Bot 2 months ago committed by Copybara-Service
parent 6c3ff5aa9e
commit ec58a34824
  1. 6
      src/google/protobuf/compiler/cpp/extension.cc
  2. 5
      src/google/protobuf/generated_message_reflection.cc
  3. 17
      src/google/protobuf/generated_message_reflection_unittest.cc
  4. 7
      src/google/protobuf/unittest.proto

@ -69,6 +69,12 @@ ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor,
variables_["constant_name"] = FieldConstantName(descriptor_);
variables_["field_type"] =
absl::StrCat(static_cast<int>(descriptor_->type()));
// Downgrade string to bytes if it is not UTF8 validated.
if (descriptor_->type() == FieldDescriptor::TYPE_STRING &&
!descriptor_->requires_utf8_validation()) {
variables_["field_type"] =
absl::StrCat(static_cast<int>(FieldDescriptor::TYPE_BYTES));
}
variables_["repeated"] = descriptor_->is_repeated() ? "true" : "false";
variables_["packed"] = descriptor_->is_packed() ? "true" : "false";
variables_["dllexport_decl"] = options.dllexport_decl;

@ -2182,7 +2182,10 @@ void Reflection::AddString(Message* message, const FieldDescriptor* field,
std::string value) const {
USAGE_MUTABLE_CHECK_ALL(AddString, REPEATED, STRING);
if (field->is_extension()) {
MutableExtensionSet(message)->AddString(field->number(), field->type(),
MutableExtensionSet(message)->AddString(field->number(),
field->requires_utf8_validation()
? FieldDescriptor::TYPE_STRING
: FieldDescriptor::TYPE_BYTES,
std::move(value), field);
} else {
switch (field->cpp_string_type()) {

@ -38,6 +38,7 @@
#include "google/protobuf/message.h"
#include "google/protobuf/port.h"
#include "google/protobuf/test_util.h"
#include "google/protobuf/text_format.h"
#include "google/protobuf/unittest.pb.h"
#include "google/protobuf/unittest_mset.pb.h"
#include "google/protobuf/unittest_mset_wire_format.pb.h"
@ -1807,6 +1808,22 @@ TEST(GeneratedMessageReflection, SwapImplicitPresenceShouldWork) {
EXPECT_EQ(lhs.child().optional_int32(), -1);
}
TEST(GeneratedMessageReflection, UnvalidatedStringsAreDowngradedToBytes) {
protobuf_unittest::TestChildExtension parsed_msg;
google::protobuf::TextFormat::ParseFromString(
R"pb(
optional_extension <
[protobuf_unittest.repeated_string_extension]: "foo"
>
)pb",
&parsed_msg);
protobuf_unittest::TestChildExtension msg;
msg.mutable_optional_extension()->AddExtension(
protobuf_unittest::repeated_string_extension, "bar");
parsed_msg.mutable_optional_extension()->Swap(
msg.mutable_optional_extension());
}
} // namespace
} // namespace protobuf
} // namespace google

@ -500,6 +500,13 @@ extend TestAllExtensions {
TestAllTypes.NestedMessage oneof_nested_message_extension = 112;
string oneof_string_extension = 113;
bytes oneof_bytes_extension = 114;
string optional_utf8_string_extension = 115 [
features.utf8_validation = VERIFY
];
repeated string repeated_utf8_string_extension = 116 [
features.utf8_validation = VERIFY
];
}
message OptionalGroup_extension {

Loading…
Cancel
Save