Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(381)

Unified Diff: test/fuzzer/parser.cc

Issue 2881583002: [fuzzer] Add input validation in the beginning of the parser fuzz target. (Closed)
Patch Set: Fix coding style and apply git cl format Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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();
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698