1 /*
2 * Copyright 2020 Endless OS Foundation, LLC
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "fuzz.h"
21
22 static void
23 test_with_flags (const gchar *data,
24 GUriFlags flags)
25 {
26 GUri *uri = NULL;
27 gchar *uri_string = NULL;
28
29 uri = g_uri_parse (data, flags, NULL);
30
31 if (uri == NULL)
32 return;
33
34 uri_string = g_uri_to_string (uri);
35 g_uri_unref (uri);
36
37 if (uri_string == NULL)
38 return;
39
40 g_free (uri_string);
41 }
42
43 int
44 LLVMFuzzerTestOneInput (const unsigned char *data, size_t size)
45 {
46 unsigned char *nul_terminated_data = NULL;
47
48 fuzz_set_logging_func ();
49
50 /* ignore @size (g_uri_parse() doesn’t support it); ensure @data is nul-terminated */
51 nul_terminated_data = (unsigned char *) g_strndup ((const gchar *) data, size);
52 test_with_flags ((const gchar *) nul_terminated_data, G_URI_FLAGS_NONE);
53 test_with_flags ((const gchar *) nul_terminated_data, G_URI_FLAGS_PARSE_RELAXED);
54 test_with_flags ((const gchar *) nul_terminated_data, G_URI_FLAGS_NON_DNS);
55 test_with_flags ((const gchar *) nul_terminated_data, G_URI_FLAGS_HAS_AUTH_PARAMS);
56 test_with_flags ((const gchar *) nul_terminated_data, G_URI_FLAGS_HAS_PASSWORD);
57 test_with_flags ((const gchar *) nul_terminated_data, G_URI_FLAGS_ENCODED_QUERY);
58 test_with_flags ((const gchar *) nul_terminated_data, G_URI_FLAGS_ENCODED_PATH);
59 test_with_flags ((const gchar *) nul_terminated_data, G_URI_FLAGS_SCHEME_NORMALIZE);
60 g_free (nul_terminated_data);
61
62 return 0;
63 }