Skip to content

Commit

Permalink
Don't overwrite customized field nullness with WithOneofAlwaysSet.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 608603586
  • Loading branch information
hadi88 authored and copybara-github committed Feb 20, 2024
1 parent cc15acf commit ad3dadf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
22 changes: 18 additions & 4 deletions domain_tests/arbitrary_domains_protobuf_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -562,19 +562,33 @@ TEST(ProtocolBuffer, WithFieldsAlwaysSetResetsWithMaxRepeatedFieldsSize) {
Gt(1))));
}

bool IsInt64(const FieldDescriptor* field) {
return field->type() == FieldDescriptor::TYPE_INT64;
}

TEST(ProtocolBuffer, ValidationRejectsIncorrectlySetOneofField) {
Domain<TestProtobuf> domain_a = Arbitrary<TestProtobuf>();
Domain<TestProtobuf> domain_b = Arbitrary<TestProtobuf>()
.WithFieldsUnset(IsInt64)
.WithOneofAlwaysSet("oneof_field")
.WithFieldUnset("oneof_u32");
TestProtobuf user_value;
user_value.set_oneof_u32(1);
auto corpus_value = domain_a.FromValue(user_value);
TestProtobuf user_value_1;
user_value_1.set_oneof_u32(1);
auto corpus_value_1 = domain_a.FromValue(user_value_1);

EXPECT_THAT(
domain_b.ValidateCorpusValue(*corpus_value),
domain_b.ValidateCorpusValue(*corpus_value_1),
IsInvalid(
"Invalid value for field oneof_u32 >> Optional value must be null"));

TestProtobuf user_value_2;
user_value_2.set_oneof_i64(1);
auto corpus_value_2 = domain_a.FromValue(user_value_2);

EXPECT_THAT(
domain_b.ValidateCorpusValue(*corpus_value_2),
IsInvalid(
"Invalid value for field oneof_i64 >> Optional value must be null"));
}

TEST(ProtocolBuffer, ValidationRejectsUnsetOneofsWithOneofAlwaysSet) {
Expand Down
7 changes: 5 additions & 2 deletions fuzztest/internal/domains/protobuf_domain_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,11 @@ class ProtobufDomainUntypedImpl
"\") should be called before customizing sub-fields.");
always_set_oneofs_.insert(oneof->index());
for (int i = 0; i < oneof->field_count(); ++i) {
SetOneofFieldPolicy(oneof->field(i), OptionalPolicy::kWithoutNull);
// Don't set previously unset fields.
if (GetPolicy().GetOptionalPolicy(oneof->field(i)) !=
OptionalPolicy::kAlwaysNull) {
SetOneofFieldPolicy(oneof->field(i), OptionalPolicy::kWithoutNull);
}
}
}

Expand Down Expand Up @@ -1227,7 +1231,6 @@ class ProtobufDomainUntypedImpl
}

ProtoPolicy<Message>& GetPolicy() {
CheckIfPolicyCanBeUpdated();
return policy_;
}

Expand Down

0 comments on commit ad3dadf

Please sign in to comment.