Index: test/fuzzer/parser.cc |
diff --git a/test/fuzzer/parser.cc b/test/fuzzer/parser.cc |
index 5870d9cb3b62873c1126c7cfce54c493d3037e5e..76666e85be36d51af4465eaa96678f8009993cba 100644 |
--- a/test/fuzzer/parser.cc |
+++ b/test/fuzzer/parser.cc |
@@ -14,7 +14,51 @@ |
#include "src/parsing/preparser.h" |
#include "test/fuzzer/fuzzer-support.h" |
+#include <cctype> |
+#include <list> |
+ |
+bool IsValidInput(const uint8_t* data, size_t size) { |
+ std::list<char> parentheses; |
+ const char* ptr = reinterpret_cast<const char*>(data); |
+ |
+ for (size_t i = 0; i != size; ++i) { |
+ // Check that all characters in the data are valid. |
+ if (!(std::isspace(ptr[i]) || std::isprint(ptr[i]))) { |
+ return false; |
+ } |
+ |
+ // Check balance of parentheses in the data. |
+ switch (ptr[i]) { |
+ case '(': |
+ case '[': |
+ case '{': |
+ parentheses.push_back(ptr[i]); |
+ break; |
+ case ')': |
+ if (parentheses.back() != '(') return false; |
+ parentheses.pop_back(); |
+ break; |
+ case ']': |
+ if (parentheses.back() != '[') return false; |
+ parentheses.pop_back(); |
+ break; |
+ case '}': |
+ if (parentheses.back() != '{') return false; |
+ parentheses.pop_back(); |
+ break; |
+ default: |
+ break; |
+ } |
+ } |
+ |
+ return parentheses.empty(); |
+} |
+ |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
+ if (!IsValidInput(data, size)) { |
+ return 0; |
+ } |
+ |
v8_fuzzer::FuzzerSupport* support = v8_fuzzer::FuzzerSupport::Get(); |
v8::Isolate* isolate = support->GetIsolate(); |