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