|
| 1 | +/* |
| 2 | +# Copyright 2024 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +################################################################################ |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <stddef.h> |
| 20 | +#include <stdint.h> |
| 21 | +#include <stdlib.h> |
| 22 | +#include <string.h> |
| 23 | + |
| 24 | +#include "yajl_lex.h" |
| 25 | +#include "yajl_alloc.h" |
| 26 | +#include "api/yajl_common.h" |
| 27 | + |
| 28 | +// Default allocation functions |
| 29 | +static void * malloc_wrapper(void *ctx, unsigned int sz) { |
| 30 | + return malloc(sz); |
| 31 | +} |
| 32 | + |
| 33 | +static void * realloc_wrapper(void *ctx, void *ptr, unsigned int sz) { |
| 34 | + return realloc(ptr, sz); |
| 35 | +} |
| 36 | + |
| 37 | +static void free_wrapper(void *ctx, void *ptr) { |
| 38 | + free(ptr); |
| 39 | +} |
| 40 | + |
| 41 | +static yajl_alloc_funcs allocFuncs = { |
| 42 | + malloc_wrapper, |
| 43 | + realloc_wrapper, |
| 44 | + free_wrapper, |
| 45 | + NULL |
| 46 | +}; |
| 47 | + |
| 48 | +// Test that peek doesn't affect subsequent lexing |
| 49 | +static void test_peek_and_lex(yajl_lexer lexer, const unsigned char* json_text, |
| 50 | + size_t json_len, unsigned int offset) { |
| 51 | + const unsigned char *outBuf1, *outBuf2; |
| 52 | + unsigned int outLen1, outLen2; |
| 53 | + unsigned int testOffset = offset; |
| 54 | + |
| 55 | + // First peek at token |
| 56 | + yajl_tok peek_tok = yajl_lex_peek(lexer, json_text, json_len, offset); |
| 57 | + |
| 58 | + // Now actually lex the token |
| 59 | + yajl_tok lex_tok = yajl_lex_lex(lexer, json_text, json_len, &testOffset, |
| 60 | + &outBuf1, &outLen1); |
| 61 | + |
| 62 | + // Verify that peek and actual lex return same token type |
| 63 | + if (peek_tok != lex_tok && peek_tok != yajl_tok_eof) { |
| 64 | + abort(); // Keep the abort to detect inconsistencies |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 69 | + if (size == 0) { |
| 70 | + return 0; |
| 71 | + } |
| 72 | + |
| 73 | + // Create lexer with different comment/UTF8 validation combinations |
| 74 | + unsigned int allowComments = data[0] & 1; |
| 75 | + unsigned int validateUTF8 = data[0] & 2; |
| 76 | + |
| 77 | + // Use explicit allocation functions instead of NULL |
| 78 | + yajl_lexer lexer = yajl_lex_alloc(&allocFuncs, allowComments, validateUTF8); |
| 79 | + if (!lexer) { |
| 80 | + return 0; |
| 81 | + } |
| 82 | + |
| 83 | + const unsigned char *json_text = data + 1; |
| 84 | + size_t json_len = size - 1; |
| 85 | + |
| 86 | + // Test peeking at different offsets through the input |
| 87 | + for (unsigned int offset = 0; offset < json_len; offset++) { |
| 88 | + test_peek_and_lex(lexer, json_text, json_len, offset); |
| 89 | + |
| 90 | + yajl_lexer new_lexer = yajl_lex_realloc(lexer); |
| 91 | + if (!new_lexer) break; |
| 92 | + lexer = new_lexer; |
| 93 | + } |
| 94 | + |
| 95 | + yajl_lex_free(lexer); |
| 96 | + return 0; |
| 97 | +} |
0 commit comments