Skip to content

Commit 36a7acd

Browse files
FuzzTest Teamcopybara-github
authored andcommitted
Fix off-by-one error in Flatbuffers domain field mutation.
The random field selection in the Flatbuffers domain was using 0-based indexing with absl::Uniform, but the mutation logic was effectively 1-based. This change aligns the random selection to be 1-based by using absl::Uniform(prng, 1ul, field_count + 1) and adjusts the field iteration logic in MutateSelectedField to correctly handle the 1-based index. Also adds a check for field_count == 0 to prevent issues with absl::Uniform. PiperOrigin-RevId: 921308530
1 parent 19f2215 commit 36a7acd

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

fuzztest/internal/domains/flatbuffers_domain_impl.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ void FlatbuffersTableUntypedDomainImpl::Mutate(
103103
CountNumberOfMutableFieldsVisitor{*this, field_count,
104104
val, only_shrink});
105105
}
106-
auto selected_field_index = absl::Uniform(prng, 0ul, field_count);
106+
if (field_count == 0) return;
107+
auto selected_field_index = absl::Uniform(prng, 1ul, field_count + 1);
107108

108109
MutateSelectedField(val, prng, metadata, only_shrink, selected_field_index);
109110
}
@@ -124,18 +125,23 @@ uint64_t FlatbuffersTableUntypedDomainImpl::MutateSelectedField(
124125
const domain_implementor::MutationMetadata& metadata, bool only_shrink,
125126
uint64_t selected_field_index) {
126127
uint64_t field_counter = 0;
128+
uint64_t fields_count = CountNumberOfFields(val);
129+
if (fields_count < selected_field_index) {
130+
return fields_count;
131+
}
132+
127133
for (const auto* field : *table_object_->fields()) {
128134
if (!IsSupportedField(field)) {
129135
if (only_shrink && !val.contains(field->id())) continue;
130136
}
131137

138+
++field_counter;
132139
if (field_counter == selected_field_index) {
133140
VisitFlatbufferField(
134141
schema_, field,
135142
MutateVisitor{*this, prng, metadata, only_shrink, val});
136143
return field_counter;
137144
}
138-
field_counter++;
139145

140146
if (field->type()->base_type() == reflection::BaseType::Obj) {
141147
auto sub_object = schema_->objects()->Get(field->type()->index());
@@ -148,7 +154,7 @@ uint64_t FlatbuffersTableUntypedDomainImpl::MutateSelectedField(
148154
// TODO: Add support for structs.
149155
}
150156

151-
if (field_counter > selected_field_index) {
157+
if (field_counter >= selected_field_index) {
152158
return field_counter;
153159
}
154160
}

0 commit comments

Comments
 (0)