xref: /netbsd-src/external/mpl/bind/dist/fuzz/isc_lex_getmastertoken.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1 /*	$NetBSD: isc_lex_getmastertoken.c,v 1.2 2024/02/21 22:51:58 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 __attribute__((unused)),
31 		     char ***argv __attribute__((unused)));
32 
33 int
34 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
35 
36 static isc_mem_t *mctx = NULL;
37 static isc_lex_t *lex = NULL;
38 
39 int
40 LLVMFuzzerInitialize(int *argc __attribute__((unused)),
41 		     char ***argv __attribute__((unused))) {
42 	isc_result_t result;
43 
44 	isc_mem_create(&mctx);
45 
46 	result = isc_lex_create(mctx, 1024, &lex);
47 	REQUIRE(result == ISC_R_SUCCESS);
48 
49 	return (0);
50 }
51 
52 int
53 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
54 	isc_buffer_t buf;
55 	isc_result_t result;
56 	isc_token_t token;
57 	isc_tokentype_t expect;
58 	bool eol;
59 
60 	if (size < sizeof(expect) + sizeof(eol)) {
61 		return (0);
62 	}
63 
64 	(void)memmove(&expect, data, sizeof(expect));
65 	data += sizeof(expect);
66 	size -= sizeof(expect);
67 
68 	eol = *data != 0;
69 	data += 1;
70 	size -= 1;
71 
72 	isc_buffer_constinit(&buf, data, size);
73 	isc_buffer_add(&buf, size);
74 	isc_buffer_setactive(&buf, size);
75 
76 	CHECK(isc_lex_openbuffer(lex, &buf));
77 
78 	do {
79 		result = isc_lex_getmastertoken(lex, &token, expect, eol);
80 	} while (result == ISC_R_SUCCESS && token.type != isc_tokentype_eof);
81 
82 	return (0);
83 }
84