File: libwget_utils_fuzzer.c

package info (click to toggle)
wget2 2.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 22,248 kB
  • sloc: ansic: 121,144; sh: 11,559; makefile: 878; xml: 182; sed: 16
file content (127 lines) | stat: -rw-r--r-- 3,559 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (c) 2017-2024 Free Software Foundation, Inc.
 *
 * This file is part of libwget.
 *
 * Libwget is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Libwget is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libwget.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <assert.h> // assert
#include <stdlib.h> // malloc, free
#include <string.h> // memcpy
// #include <unistd.h> // chroot

#include "wget.h"
#include "fuzzer.h"

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
	char dst1[1];
	char dst2[2];
	char dst3[3];
	char dst4[4];
	char dst5[8];
	char *dst = (char *) malloc(size * 2 + 1);
	char *data0 = (char *) malloc(size + 1);
	int x = 0; // avoid pure functions to be optimized away

	assert(dst != NULL);
	assert(data0 != NULL);

	// 0-terminate data
	memcpy(data0, data, size);
	data0[size] = 0;

	// some test for code coverage
	x += wget_strcmp(NULL, "");
	x += wget_strcmp("", NULL);
	x += wget_strcmp(NULL, NULL);
	x += wget_strcmp(data0, data0);

	x +=  wget_strncmp(NULL, "", 0);
	x += wget_strncmp("", NULL, 0);
	x += wget_strncmp(NULL, NULL, 0);
	x += wget_strncmp((char *) data, (char *) data, size);

	x += wget_strcasecmp(NULL, "");
	x += wget_strcasecmp("", NULL);
	x += wget_strcasecmp(NULL, NULL);
	x += wget_strcasecmp(data0, data0);

	x += wget_strncasecmp(NULL, "", 0);
	x += wget_strncasecmp("", NULL, 0);
	x += wget_strncasecmp(NULL, NULL, 0);
	x += wget_strncasecmp((char *) data, (char *) data, size);

	x += wget_strcasecmp_ascii(NULL, "");
	x += wget_strcasecmp_ascii("", NULL);
	x += wget_strcasecmp_ascii(NULL, NULL);
	x += wget_strcasecmp_ascii(data0, data0);

	x += wget_strncasecmp_ascii(NULL, "", 0);
	x += wget_strncasecmp_ascii("", NULL, 0);
	x += wget_strncasecmp_ascii(NULL, NULL, 0);
	x += wget_strncasecmp_ascii((char *) data, (char *) data, size);

	wget_strtolower(NULL);
	wget_strtolower(data0);
	memcpy(data0, (char *) data, size); // restore

	wget_millisleep(-1);
	wget_get_timemillis();

	wget_percent_unescape(data0);
	memcpy(data0, data, size); // restore

	x += wget_match_tail(data0, data0);
	x += wget_match_tail("", data0);
	x += wget_match_tail(data0, "");

	x += wget_match_tail_nocase(data0, data0);
	x += wget_match_tail_nocase("", data0);
	x += wget_match_tail_nocase(data0, "");

//	if (chroot(".") == 0) {
		char *p;
		if ((p = wget_strnglob("*", 1,  0)))
			wget_free(p);
//	} else
//		printf("Failed to chroot\n");

	if (size < 31) {
		char buf[16];
		x += !!wget_human_readable(dst1, sizeof(dst1), (1 << size) - 1);
		x += !!wget_human_readable(buf, sizeof(buf), (1 << size) - 1);
	}

	(void) x; // needed to get rid of bug reported by scan-build

	int w, h;
	wget_get_screen_size(&w, &h);

	wget_memtohex(NULL, 0, NULL, 0);
	wget_memtohex(data, size, dst1, sizeof(dst1));
	wget_memtohex(data, size, dst2, sizeof(dst2));
	wget_memtohex(data, size, dst3, sizeof(dst3));
	wget_memtohex(data, size, dst4, sizeof(dst4));
	wget_memtohex(data, size, dst5, sizeof(dst5));
	wget_memtohex(data, size, dst, size * 2 + 1);

	free(data0);
	free(dst);

	return 0;
}