Fix utf8 mode check to always return kNone for non-string fields.

This is functionally equivalent, but avoids a codegen change under editions where the inherited value of utf8_validation can change for non-string maps.

PiperOrigin-RevId: 586493296
pull/14871/head
Mike Kruskal 1 year ago
parent 454a780602
commit 3e61525e6b
  1. 19
      src/google/protobuf/descriptor.cc
  2. 18
      src/google/protobuf/descriptor_unittest.cc
  3. 8
      src/google/protobuf/generated_message_tctable_gen.cc

@ -9576,13 +9576,18 @@ static bool IsVerifyUtf8(const FieldDescriptor* field, bool is_lite) {
// Which level of UTF-8 enforcemant is placed on this file.
Utf8CheckMode GetUtf8CheckMode(const FieldDescriptor* field, bool is_lite) {
if (IsStrictUtf8(field)) {
return Utf8CheckMode::kStrict;
} else if (IsVerifyUtf8(field, is_lite)) {
return Utf8CheckMode::kVerify;
} else {
return Utf8CheckMode::kNone;
}
if (field->type() == FieldDescriptor::TYPE_STRING ||
(field->is_map() && (field->message_type()->map_key()->type() ==
FieldDescriptor::TYPE_STRING ||
field->message_type()->map_value()->type() ==
FieldDescriptor::TYPE_STRING))) {
if (IsStrictUtf8(field)) {
return Utf8CheckMode::kStrict;
} else if (IsVerifyUtf8(field, is_lite)) {
return Utf8CheckMode::kVerify;
}
}
return Utf8CheckMode::kNone;
}
bool IsLazilyInitializedFile(absl::string_view filename) {

@ -7411,8 +7411,11 @@ TEST_F(FeaturesTest, Proto2Features) {
[pb.cpp] { legacy_closed_enum: true })pb"));
EXPECT_TRUE(field->has_presence());
EXPECT_FALSE(field->requires_utf8_validation());
EXPECT_EQ(GetUtf8CheckMode(field, /*is_lite=*/false), Utf8CheckMode::kVerify);
EXPECT_EQ(GetUtf8CheckMode(field, /*is_lite=*/true), Utf8CheckMode::kNone);
EXPECT_EQ(
GetUtf8CheckMode(message->FindFieldByName("str"), /*is_lite=*/false),
Utf8CheckMode::kVerify);
EXPECT_EQ(GetUtf8CheckMode(message->FindFieldByName("str"), /*is_lite=*/true),
Utf8CheckMode::kNone);
EXPECT_FALSE(field->is_packed());
EXPECT_FALSE(field->legacy_enum_field_treated_as_closed());
EXPECT_FALSE(HasPreservingUnknownEnumSemantics(field));
@ -7477,8 +7480,11 @@ TEST_F(FeaturesTest, Proto3Features) {
[pb.cpp] { legacy_closed_enum: false })pb"));
EXPECT_FALSE(field->has_presence());
EXPECT_FALSE(field->requires_utf8_validation());
EXPECT_EQ(GetUtf8CheckMode(field, /*is_lite=*/false), Utf8CheckMode::kStrict);
EXPECT_EQ(GetUtf8CheckMode(field, /*is_lite=*/true), Utf8CheckMode::kStrict);
EXPECT_EQ(
GetUtf8CheckMode(message->FindFieldByName("str"), /*is_lite=*/false),
Utf8CheckMode::kStrict);
EXPECT_EQ(GetUtf8CheckMode(message->FindFieldByName("str"), /*is_lite=*/true),
Utf8CheckMode::kStrict);
EXPECT_FALSE(field->is_packed());
EXPECT_FALSE(field->legacy_enum_field_treated_as_closed());
EXPECT_FALSE(HasPreservingUnknownEnumSemantics(field));
@ -9214,9 +9220,9 @@ TEST_F(FeaturesTest, FieldFeatureHelpers) {
EXPECT_FALSE(default_repeated_field->has_presence());
EXPECT_FALSE(default_repeated_field->requires_utf8_validation());
EXPECT_EQ(GetUtf8CheckMode(default_repeated_field, /*is_lite=*/false),
Utf8CheckMode::kStrict);
Utf8CheckMode::kNone);
EXPECT_EQ(GetUtf8CheckMode(default_repeated_field, /*is_lite=*/true),
Utf8CheckMode::kStrict);
Utf8CheckMode::kNone);
EXPECT_TRUE(required_field->has_presence());
EXPECT_TRUE(required_field->is_required());

@ -412,13 +412,7 @@ std::vector<TailCallTableInfo::FastFieldInfo> SplitFastFieldsForSize(
// We only need field names for reporting UTF-8 parsing errors, so we only
// emit them for string fields with Utf8 transform specified.
bool NeedsFieldNameForTable(const FieldDescriptor* field, bool is_lite) {
if (cpp::GetUtf8CheckMode(field, is_lite) == cpp::Utf8CheckMode::kNone)
return false;
return field->type() == FieldDescriptor::TYPE_STRING ||
(field->is_map() && (field->message_type()->map_key()->type() ==
FieldDescriptor::TYPE_STRING ||
field->message_type()->map_value()->type() ==
FieldDescriptor::TYPE_STRING));
return cpp::GetUtf8CheckMode(field, is_lite) != cpp::Utf8CheckMode::kNone;
}
absl::string_view FieldNameForTable(

Loading…
Cancel
Save