1 /*
2  * Copyright (c) 2017-2021 Free Software Foundation, Inc.
3  *
4  * This file is part of libwget.
5  *
6  * Libwget is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Libwget is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with libwget.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include <config.h>
21 
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "wget.h"
28 #include "fuzzer.h"
29 
30 static const uint8_t *g_data;
31 static size_t g_size;
32 
33 #if defined HAVE_DLFCN_H && defined HAVE_FMEMOPEN
34 #include <dlfcn.h>
35 #ifdef RTLD_NEXT /* Not defined e.g. on CygWin */
fopen(const char * pathname,const char * mode)36 FILE *fopen(const char *pathname, const char *mode)
37 {
38 	FILE *(*libc_fopen)(const char *, const char *) =
39 		(FILE *(*)(const char *, const char *)) dlsym (RTLD_NEXT, "fopen");
40 
41 	if (!strcmp(pathname, "netrc"))
42 		return fmemopen((void *) g_data, g_size, mode);
43 
44 	return libc_fopen(pathname, mode);
45 }
46 #endif
47 #endif
48 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)49 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
50 {
51 	if (size > 256) // same as max_len = 256 in .options file
52 		return 0;
53 
54 	g_data = data;
55 	g_size = size;
56 
57 	wget_netrc_db *netrc_db = wget_netrc_db_init(NULL);
58 #if ! defined _WIN32 && defined HAVE_FMEMOPEN
59 	wget_netrc_db_load(netrc_db, "netrc");
60 #endif
61 	wget_netrc_get(netrc_db, "x.y");
62 	wget_netrc_db_free(&netrc_db);
63 
64 	return 0;
65 }
66