Fix OSS-Fuzz build breakage
The pw_tokenizer module fuzzers were using a few APIs in clang11.0
headers (which is what we locally test against) which are missing
in clang10.0 that the ClusterFuzz builds fuzz targets with.
Change-Id: Idcda9e8af46b6091f3f3eb835e246cd436c04165
diff --git a/pw_tokenizer/detokenize_fuzzer.cc b/pw_tokenizer/detokenize_fuzzer.cc
index a473e14..76e3a7f 100644
--- a/pw_tokenizer/detokenize_fuzzer.cc
+++ b/pw_tokenizer/detokenize_fuzzer.cc
@@ -82,7 +82,8 @@
}
case kStringView: {
- std::string str = provider.ConsumeRandomLengthString();
+ std::string str =
+ provider.ConsumeRandomLengthString(provider.remaining_bytes());
auto detokenized_string = detokenizer.Detokenize(str);
PW_UNUSED(detokenized_string);
break;
diff --git a/pw_tokenizer/token_database_fuzzer.cc b/pw_tokenizer/token_database_fuzzer.cc
index 2013387..0fb5d57 100644
--- a/pw_tokenizer/token_database_fuzzer.cc
+++ b/pw_tokenizer/token_database_fuzzer.cc
@@ -60,6 +60,7 @@
PW_UNUSED(entry_token);
}
}
+
} // namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
@@ -98,14 +99,17 @@
// Consume the remaining data. Note that the data corresponding to the
// string entries in the database are not explicitly null-terminated.
- size_t data_bytes_consumed = provider.ConsumeData(buffer + kTokenHeaderSize,
- provider.remaining_bytes());
+ // TODO(karthikmb): Once OSS-Fuzz updates to Clang11.0, switch to
+ // provider.ConsumeData() to avoid extra memory and the memcpy call.
+ auto consumed_bytes =
+ provider.ConsumeBytes<uint8_t>(provider.remaining_bytes());
+ memcpy(buffer + kTokenHeaderSize, &consumed_bytes[0], consumed_bytes.size());
SetTokenEntryCountInBuffer(buffer, random_token_count);
// Poison the unused buffer space for this run of the fuzzer to
// prevent the token database creator from reading too far in.
- size_t data_size = kTokenHeaderSize + data_bytes_consumed;
+ size_t data_size = kTokenHeaderSize + consumed_bytes.size();
size_t poisoned_length = kBufferSizeMax - data_size;
void* poisoned = &buffer[data_size];