/src/libpsl/fuzz/libpsl_fuzzer.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: MIT |
3 | | * |
4 | | * See the LICENSE file in the root directory for details and copyrights. |
5 | | * |
6 | | * This file is part of libpsl. |
7 | | * |
8 | | */ |
9 | | |
10 | | #include <config.h> |
11 | | |
12 | | #include <assert.h> /* assert */ |
13 | | |
14 | | #ifdef HAVE_STDINT_H |
15 | | #include <stdint.h> /* uint8_t */ |
16 | | #elif defined (_MSC_VER) |
17 | | typedef unsigned __int8 uint8_t; |
18 | | #endif |
19 | | |
20 | | #include <stdlib.h> /* malloc, free */ |
21 | | #include <string.h> /* memcpy */ |
22 | | |
23 | | #ifdef WITH_LIBICU |
24 | | #ifndef WITH_LIBICU_WIN |
25 | | #include <unicode/uclean.h> |
26 | | #else |
27 | | #include <icu.h> |
28 | | #endif |
29 | | #endif |
30 | | |
31 | | #include "libpsl.h" |
32 | | #include "fuzzer.h" |
33 | | |
34 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
35 | 1.36k | { |
36 | 1.36k | static int first_run = 1; |
37 | 1.36k | psl_ctx_t *psl; |
38 | 1.36k | char *domain, *res; |
39 | 1.36k | int rc; |
40 | | |
41 | 1.36k | if (size > 64 * 1024 - 1) |
42 | 6 | return 0; |
43 | | |
44 | 1.36k | domain = (char *) malloc(size + 1); |
45 | 1.36k | assert(domain != NULL); |
46 | | |
47 | | /* 0 terminate */ |
48 | 1.36k | memcpy(domain, data, size); |
49 | 1.36k | domain[size] = 0; |
50 | | |
51 | 1.36k | psl = (psl_ctx_t *) psl_builtin(); |
52 | | |
53 | 1.36k | psl_is_public_suffix(psl, domain); |
54 | 1.36k | psl_is_public_suffix2(psl, domain, PSL_TYPE_PRIVATE); |
55 | 1.36k | psl_is_public_suffix2(psl, domain, PSL_TYPE_ICANN); |
56 | 1.36k | psl_is_public_suffix2(psl, domain, PSL_TYPE_NO_STAR_RULE); |
57 | 1.36k | psl_is_public_suffix2(psl, domain, PSL_TYPE_NO_STAR_RULE|PSL_TYPE_ANY); |
58 | 1.36k | psl_unregistrable_domain(psl, domain); |
59 | 1.36k | psl_registrable_domain(psl, domain); |
60 | | |
61 | 1.36k | psl_is_cookie_domain_acceptable(psl, "", NULL); |
62 | 1.36k | psl_is_cookie_domain_acceptable(psl, "a.b.c.e.com", domain); |
63 | | |
64 | 1.36k | if ((rc = psl_str_to_utf8lower(domain, "utf-8", NULL, &res)) == PSL_SUCCESS) |
65 | 1.36k | free(res); |
66 | 1.36k | if ((rc = psl_str_to_utf8lower(domain, "iso-8859-1", NULL, &res)) == PSL_SUCCESS) |
67 | 1.36k | free(res); |
68 | 1.36k | if ((rc = psl_str_to_utf8lower(domain, NULL, NULL, &res)) == PSL_SUCCESS) |
69 | 1.36k | free(res); |
70 | | |
71 | 1.36k | psl_free(psl); |
72 | | |
73 | 1.36k | if (first_run) { |
74 | 1 | psl_is_public_suffix(NULL, domain); |
75 | 1 | psl_check_version_number(1); |
76 | 1 | psl_get_version(); |
77 | 1 | psl_dist_filename(); |
78 | 1 | psl_builtin_outdated(); |
79 | 1 | psl_builtin_filename(); |
80 | 1 | psl_builtin_sha1sum(); |
81 | 1 | psl_builtin_file_time(); |
82 | 1 | first_run = 0; |
83 | 1 | } |
84 | | |
85 | 1.36k | free(domain); |
86 | | |
87 | 1.36k | #if defined(WITH_LIBICU) |
88 | 1.36k | u_cleanup(); /* free all library internal memory to avoid memory leaks being reported */ |
89 | 1.36k | #endif |
90 | | |
91 | 1.36k | return 0; |
92 | 1.36k | } |