diff options
Diffstat (limited to 'examples/libfuzzer/libfuzzer_example.cc')
-rw-r--r-- | examples/libfuzzer/libfuzzer_example.cc | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/examples/libfuzzer/libfuzzer_example.cc b/examples/libfuzzer/libfuzzer_example.cc index 6eab362..aa65125 100644 --- a/examples/libfuzzer/libfuzzer_example.cc +++ b/examples/libfuzzer/libfuzzer_example.cc @@ -13,6 +13,7 @@ // limitations under the License. #include <cmath> +#include <iostream> #include "examples/libfuzzer/libfuzzer_example.pb.h" #include "port/protobuf.h" @@ -20,32 +21,30 @@ protobuf_mutator::protobuf::LogSilencer log_silincer; -DEFINE_PROTO_FUZZER(const libfuzzer_example::Msg& message) { - static PostProcessorRegistration reg = { - [](libfuzzer_example::Msg* message, unsigned int seed) { - if (seed % 2) { - message->set_optional_uint64( - std::hash<std::string>{}(message->optional_string())); - } - - if (message->has_any()) { - auto* any = message->mutable_any(); - - // Guide mutator to usefull 'Any' types. - static const char* const expected_types[] = { - "type.googleapis.com/google.protobuf.DescriptorProto", - "type.googleapis.com/google.protobuf.FileDescriptorProto", - }; - - if (!std::count(std::begin(expected_types), std::end(expected_types), - any->type_url())) { - const size_t num = - (std::end(expected_types) - std::begin(expected_types)); - any->set_type_url(expected_types[seed % num]); - } +protobuf_mutator::libfuzzer::PostProcessorRegistration<libfuzzer_example::Msg> + reg = {[](libfuzzer_example::Msg* message, unsigned int seed) { + message->set_optional_uint64( + std::hash<std::string>{}(message->optional_string())); + + if (message->has_any()) { + auto* any = message->mutable_any(); + + // Guide mutator to usefull 'Any' types. + static const char* const expected_types[] = { + "type.googleapis.com/google.protobuf.DescriptorProto", + "type.googleapis.com/google.protobuf.FileDescriptorProto", + }; + + if (!std::count(std::begin(expected_types), std::end(expected_types), + any->type_url())) { + const size_t num = + (std::end(expected_types) - std::begin(expected_types)); + any->set_type_url(expected_types[seed % num]); } - }}; + } + }}; +DEFINE_PROTO_FUZZER(const libfuzzer_example::Msg& message) { protobuf_mutator::protobuf::FileDescriptorProto file; // Emulate a bug. |