From fdec1183124a7654fc486af690b856c30fb1ba0a Mon Sep 17 00:00:00 2001 From: Adam Korczynski Date: Thu, 9 Apr 2020 16:21:56 +0100 Subject: [PATCH] Added fuzzer for stb_c_lexer --- tests/stb_c_lexer_fuzzer.cpp | 74 ++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tests/stb_c_lexer_fuzzer.cpp diff --git a/tests/stb_c_lexer_fuzzer.cpp b/tests/stb_c_lexer_fuzzer.cpp new file mode 100644 index 0000000..f6dd04a --- /dev/null +++ b/tests/stb_c_lexer_fuzzer.cpp @@ -0,0 +1,74 @@ +#define STB_C_LEX_C_DECIMAL_INTS Y +#define STB_C_LEX_C_HEX_INTS Y +#define STB_C_LEX_C_OCTAL_INTS Y +#define STB_C_LEX_C_DECIMAL_FLOATS Y +#define STB_C_LEX_C99_HEX_FLOATS Y +#define STB_C_LEX_C_IDENTIFIERS Y +#define STB_C_LEX_C_DQ_STRINGS Y +#define STB_C_LEX_C_SQ_STRINGS Y +#define STB_C_LEX_C_CHARS Y +#define STB_C_LEX_C_COMMENTS Y +#define STB_C_LEX_CPP_COMMENTS Y +#define STB_C_LEX_C_COMPARISONS Y +#define STB_C_LEX_C_LOGICAL Y +#define STB_C_LEX_C_SHIFTS Y +#define STB_C_LEX_C_INCREMENTS Y +#define STB_C_LEX_C_ARROW Y +#define STB_C_LEX_EQUAL_ARROW Y +#define STB_C_LEX_C_BITWISEEQ Y +#define STB_C_LEX_C_ARITHEQ Y + +#define STB_C_LEX_PARSE_SUFFIXES Y +#define STB_C_LEX_DECIMAL_SUFFIXES "uUlL" +#define STB_C_LEX_HEX_SUFFIXES "lL" +#define STB_C_LEX_OCTAL_SUFFIXES "lL" +#define STB_C_LEX_FLOAT_SUFFIXES "uulL" + +#define STB_C_LEX_0_IS_EOF N +#define STB_C_LEX_INTEGERS_AS_DOUBLES N +#define STB_C_LEX_MULTILINE_DSTRINGS Y +#define STB_C_LEX_MULTILINE_SSTRINGS Y +#define STB_C_LEX_USE_STDLIB N +#define STB_C_LEX_DOLLAR_IDENTIFIER Y +#define STB_C_LEX_FLOAT_NO_DECIMAL Y + +#define STB_C_LEX_DEFINE_ALL_TOKEN_NAMES Y +#define STB_C_LEX_DISCARD_PREPROCESSOR Y +#define STB_C_LEXER_DEFINITIONS + +#define STB_C_LEXER_IMPLEMENTATION +#define STB_C_LEXER_SELF_TEST +#include "../stb_c_lexer.h" +#include +#include +#include +#include + + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if(size<3){ + return 0; + } + char *input_stream = (char *)malloc(size); + if (input_stream == NULL){ + return 0; + } + memcpy(input_stream, data, size); + + stb_lexer lex; + char *input_end = input_stream+size-1; + char *store = (char *)malloc(0x10000); + int len = 0x10000; + + stb_c_lexer_init(&lex, input_stream, input_end, store, len); + while (stb_c_lexer_get_token(&lex)) { + if (lex.token == CLEX_parse_error) { + break; + } + } + + free(input_stream); + free(store); + return 0; +}