/src/bind9/fuzz/isc_lex_getmastertoken.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 |
5 | | * |
6 | | * This Source Code Form is subject to the terms of the Mozilla Public |
7 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 | | * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
9 | | * |
10 | | * See the COPYRIGHT file distributed with this work for additional |
11 | | * information regarding copyright ownership. |
12 | | */ |
13 | | |
14 | | #include <stddef.h> |
15 | | #include <stdint.h> |
16 | | |
17 | | #include <isc/buffer.h> |
18 | | #include <isc/lex.h> |
19 | | #include <isc/mem.h> |
20 | | #include <isc/string.h> |
21 | | #include <isc/util.h> |
22 | | |
23 | | #include "fuzz.h" |
24 | | |
25 | | bool debug = false; |
26 | | |
27 | | int |
28 | | LLVMFuzzerInitialize(int *argc ISC_ATTR_UNUSED, char ***argv ISC_ATTR_UNUSED); |
29 | | |
30 | | int |
31 | | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
32 | | |
33 | | static isc_mem_t *mctx = NULL; |
34 | | static isc_lex_t *lex = NULL; |
35 | | |
36 | | int |
37 | 18 | LLVMFuzzerInitialize(int *argc ISC_ATTR_UNUSED, char ***argv ISC_ATTR_UNUSED) { |
38 | 18 | isc_mem_create("fuzz", &mctx); |
39 | 18 | isc_lex_create(mctx, 1024, &lex); |
40 | | |
41 | 18 | return 0; |
42 | 18 | } |
43 | | |
44 | | int |
45 | 950 | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
46 | 950 | isc_buffer_t buf; |
47 | 950 | isc_result_t result; |
48 | 950 | isc_token_t token; |
49 | 950 | isc_tokentype_t expect; |
50 | 950 | bool eol; |
51 | | |
52 | 950 | if (size < sizeof(expect) + sizeof(eol)) { |
53 | 4 | return 0; |
54 | 4 | } |
55 | | |
56 | 946 | (void)memmove(&expect, data, sizeof(expect)); |
57 | 946 | data += sizeof(expect); |
58 | 946 | size -= sizeof(expect); |
59 | | |
60 | 946 | eol = *data != 0; |
61 | 946 | data += 1; |
62 | 946 | size -= 1; |
63 | | |
64 | 946 | isc_buffer_constinit(&buf, data, size); |
65 | 946 | isc_buffer_add(&buf, size); |
66 | 946 | isc_buffer_setactive(&buf, size); |
67 | | |
68 | 946 | CHECK(isc_lex_openbuffer(lex, &buf)); |
69 | | |
70 | 11.5k | do { |
71 | 11.5k | result = isc_lex_getmastertoken(lex, &token, expect, eol); |
72 | 11.5k | } while (result == ISC_R_SUCCESS && token.type != isc_tokentype_eof); |
73 | | |
74 | 946 | cleanup: |
75 | 946 | return 0; |
76 | 946 | } |