Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions domain_tests/container_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,29 @@ TEST(Container, MaxSizeAndSoftMaxSizeOverwriteEachOther) {
EXPECT_TRUE(domain_soft.ValidateCorpusValue(*corpus_val_soft).ok());
}

TEST(ContainerDeathTest, MutationAbortsWhenSizeExceedsMaxSize) {
TEST(ContainerTest,
MutationDoesNotAbortWhenSizeExceedsMaxSizeButEnforcesShrink) {
auto domain_hard = Arbitrary<std::vector<int>>().WithMaxSize(2);
std::vector<int> value = {1, 2, 3};
Value val(domain_hard, value);
absl::BitGen bitgen;

EXPECT_DEATH(
val.Mutate(domain_hard, bitgen, /*metadata=*/{}, /*only_shrink=*/false),
"Size 3 is not between 0 and 2");
// Should not crash, despite size 3 > max_size 2.
val.Mutate(domain_hard, bitgen, /*metadata=*/{}, /*only_shrink=*/false);

// Size should not have grown.
EXPECT_LE(val.user_value.size(), 3);

// If we mutate enough times, it should eventually shrink to <= 2.
bool shrunk = false;
for (int i = 0; i < 1000; ++i) {
val.Mutate(domain_hard, bitgen, /*metadata=*/{}, /*only_shrink=*/false);
if (val.user_value.size() <= 2) {
shrunk = true;
break;
}
}
EXPECT_TRUE(shrunk);
}

TEST(ContainerTest, WithSoftMaxSizeMutationDoesNotGrowIfSizeExceedsLimit) {
Expand Down
12 changes: 4 additions & 8 deletions fuzztest/internal/domains/container_of_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,10 @@ class ContainerOfImplBase
const domain_implementor::MutationMetadata& metadata,
bool only_shrink) {
permanent_dict_candidate_ = std::nullopt;
if (!validate_max_size()) {
FUZZTEST_CHECK(min_size() <= val.size())
<< "Size " << val.size() << " is smaller than min size "
<< min_size();
} else {
FUZZTEST_CHECK(min_size() <= val.size() && val.size() <= max_size())
<< "Size " << val.size() << " is not between " << min_size()
<< " and " << max_size();
FUZZTEST_CHECK(min_size() <= val.size())
<< "Size " << val.size() << " is smaller than min size " << min_size();
if (validate_max_size() && val.size() > max_size()) {
only_shrink = true;
}

const bool can_shrink = val.size() > min_size();
Expand Down
Loading