Skip to content

Commit cf98842

Browse files
committed
Fix nested post-processor example
Fixes #164.
1 parent e33a10c commit cf98842

File tree

3 files changed

+53
-47
lines changed

3 files changed

+53
-47
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,20 @@ may corrupt the reproducer so it stops triggering the bug.
118118
Note: You can add callback for any nested message and you can add multiple callbacks for
119119
the same message type.
120120
```
121-
DEFINE_PROTO_FUZZER(const MyMessageType& input) {
122-
static PostProcessorRegistration reg1 = {
123-
[](MyMessageType* message, unsigned int seed) {
124-
TweakMyMessage(message, seed);
125-
}};
126-
static PostProcessorRegistration reg2 = {
127-
[](MyMessageType* message, unsigned int seed) {
128-
DifferentTweakMyMessage(message, seed);
129-
}};
130-
static PostProcessorRegistration reg_nested = {
131-
[](MyMessageType::Nested* message, unsigned int seed) {
132-
TweakMyNestedMessage(message, seed);
133-
}};
121+
static PostProcessorRegistration<MyMessageType> reg1 = {
122+
[](MyMessageType* message, unsigned int seed) {
123+
TweakMyMessage(message, seed);
124+
}};
125+
static PostProcessorRegistration<MyMessageType> reg2 = {
126+
[](MyMessageType* message, unsigned int seed) {
127+
DifferentTweakMyMessage(message, seed);
128+
}};
129+
static PostProcessorRegistration<MyMessageType::Nested> reg_nested = {
130+
[](MyMessageType::Nested* message, unsigned int seed) {
131+
TweakMyNestedMessage(message, seed);
132+
}};
134133
134+
DEFINE_PROTO_FUZZER(const MyMessageType& input) {
135135
// Code which needs to be fuzzed.
136136
ConsumeMyMessageType(input);
137137
}

examples/libfuzzer/libfuzzer_bin_example.cc

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,29 @@
2121

2222
protobuf_mutator::protobuf::LogSilencer log_silincer;
2323

24-
protobuf_mutator::libfuzzer::PostProcessorRegistration<libfuzzer_example::Msg>
25-
reg = {[](libfuzzer_example::Msg* message, unsigned int seed) {
24+
template <class Proto>
25+
using PostProcessor =
26+
protobuf_mutator::libfuzzer::PostProcessorRegistration<Proto>;
27+
28+
static PostProcessor<libfuzzer_example::Msg> reg1 = {
29+
[](libfuzzer_example::Msg* message, unsigned int seed) {
2630
message->set_optional_uint64(
2731
std::hash<std::string>{}(message->optional_string()));
32+
}};
2833

29-
if (message->has_any()) {
30-
auto* any = message->mutable_any();
31-
32-
// Guide mutator to usefull 'Any' types.
33-
static const char* const expected_types[] = {
34-
"type.googleapis.com/google.protobuf.DescriptorProto",
35-
"type.googleapis.com/google.protobuf.FileDescriptorProto",
36-
};
37-
38-
if (!std::count(std::begin(expected_types), std::end(expected_types),
39-
any->type_url())) {
40-
const size_t num =
41-
(std::end(expected_types) - std::begin(expected_types));
42-
any->set_type_url(expected_types[seed % num]);
43-
}
34+
static PostProcessor<google::protobuf::Any> reg2 = {
35+
[](google::protobuf::Any* any, unsigned int seed) {
36+
// Guide mutator to usefull 'Any' types.
37+
static const char* const expected_types[] = {
38+
"type.googleapis.com/google.protobuf.DescriptorProto",
39+
"type.googleapis.com/google.protobuf.FileDescriptorProto",
40+
};
41+
42+
if (!std::count(std::begin(expected_types), std::end(expected_types),
43+
any->type_url())) {
44+
const size_t num =
45+
(std::end(expected_types) - std::begin(expected_types));
46+
any->set_type_url(expected_types[seed % num]);
4447
}
4548
}};
4649

examples/libfuzzer/libfuzzer_example.cc

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,29 @@
2121

2222
protobuf_mutator::protobuf::LogSilencer log_silincer;
2323

24-
protobuf_mutator::libfuzzer::PostProcessorRegistration<libfuzzer_example::Msg>
25-
reg = {[](libfuzzer_example::Msg* message, unsigned int seed) {
24+
template <class Proto>
25+
using PostProcessor =
26+
protobuf_mutator::libfuzzer::PostProcessorRegistration<Proto>;
27+
28+
static PostProcessor<libfuzzer_example::Msg> reg1 = {
29+
[](libfuzzer_example::Msg* message, unsigned int seed) {
2630
message->set_optional_uint64(
2731
std::hash<std::string>{}(message->optional_string()));
32+
}};
2833

29-
if (message->has_any()) {
30-
auto* any = message->mutable_any();
31-
32-
// Guide mutator to usefull 'Any' types.
33-
static const char* const expected_types[] = {
34-
"type.googleapis.com/google.protobuf.DescriptorProto",
35-
"type.googleapis.com/google.protobuf.FileDescriptorProto",
36-
};
37-
38-
if (!std::count(std::begin(expected_types), std::end(expected_types),
39-
any->type_url())) {
40-
const size_t num =
41-
(std::end(expected_types) - std::begin(expected_types));
42-
any->set_type_url(expected_types[seed % num]);
43-
}
34+
static PostProcessor<google::protobuf::Any> reg2 = {
35+
[](google::protobuf::Any* any, unsigned int seed) {
36+
// Guide mutator to usefull 'Any' types.
37+
static const char* const expected_types[] = {
38+
"type.googleapis.com/google.protobuf.DescriptorProto",
39+
"type.googleapis.com/google.protobuf.FileDescriptorProto",
40+
};
41+
42+
if (!std::count(std::begin(expected_types), std::end(expected_types),
43+
any->type_url())) {
44+
const size_t num =
45+
(std::end(expected_types) - std::begin(expected_types));
46+
any->set_type_url(expected_types[seed % num]);
4447
}
4548
}};
4649

0 commit comments

Comments
 (0)