diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index 0f6964d82a..3b10988141 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.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) { diff --git a/src/google/protobuf/descriptor_unittest.cc b/src/google/protobuf/descriptor_unittest.cc index 323c8d146a..623229e410 100644 --- a/src/google/protobuf/descriptor_unittest.cc +++ b/src/google/protobuf/descriptor_unittest.cc @@ -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()); diff --git a/src/google/protobuf/generated_message_tctable_gen.cc b/src/google/protobuf/generated_message_tctable_gen.cc index 1a64e6ee98..b421a355b3 100644 --- a/src/google/protobuf/generated_message_tctable_gen.cc +++ b/src/google/protobuf/generated_message_tctable_gen.cc @@ -412,13 +412,7 @@ std::vector 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(