Coverage Report

Created: 2024-03-08 06:32

/src/wget2/fuzz/libwget_iri_fuzzer.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2017-2024 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 <assert.h>
23
#include <stdio.h>
24
#include <stdint.h>
25
#include <stdlib.h>
26
#include <string.h>
27
28
#include "wget.h"
29
#include "fuzzer.h"
30
31
static void test(char *in, size_t len, const char *encoding)
32
8.39k
{
33
8.39k
  wget_iri *base;
34
8.39k
  base = wget_iri_parse("http://x.org", encoding);
35
8.39k
  assert(base != NULL);
36
37
0
  wget_iri *iri, *iri2;
38
8.39k
  iri = wget_iri_parse(in, encoding);
39
8.39k
  iri2 = wget_iri_clone(iri);
40
8.39k
  wget_iri_free(&iri2);
41
8.39k
  iri2 = wget_iri_parse_base(NULL, in, encoding);
42
8.39k
  wget_iri_free(&iri2);
43
8.39k
  iri2 = wget_iri_parse_base(base, in, encoding);
44
8.39k
  int x = wget_iri_compare(iri, iri2);
45
8.39k
  wget_iri_free(&iri2);
46
47
8.39k
  wget_buffer buf;
48
8.39k
  wget_buffer_init(&buf, NULL, 32);
49
8.39k
  wget_buffer_printf(&buf, "%d", x); // use x to avoid optimization (removal of call to wget_iri_compare)
50
51
8.39k
  wget_iri_relative_to_abs(base, (const char *) in, len, &buf);
52
8.39k
  wget_iri_escape(in, &buf);
53
8.39k
  wget_iri_escape_path(in, &buf);
54
8.39k
  wget_iri_escape_query(in, &buf);
55
8.39k
  if (iri) {
56
7.32k
    if (wget_iri_supported(iri))
57
7.32k
      wget_iri_set_scheme(iri, WGET_IRI_SCHEME_HTTPS);
58
7.32k
    wget_iri_get_escaped_host(iri, &buf);
59
7.32k
    wget_iri_get_escaped_resource(iri, &buf);
60
7.32k
    wget_iri_get_path(iri, &buf, encoding);
61
7.32k
    wget_iri_get_query_as_filename(iri, &buf, encoding);
62
7.32k
    wget_iri_get_basename(iri, &buf, encoding, WGET_IRI_WITH_QUERY);
63
7.32k
    wget_iri_get_connection_part(iri, &buf);
64
7.32k
  }
65
66
8.39k
  wget_buffer_deinit(&buf);
67
8.39k
  wget_iri_free(&iri);
68
8.39k
  wget_iri_free(&base);
69
8.39k
}
70
71
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
72
4.20k
{
73
4.20k
  if (size > 10000) // same as max_len = 10000 in .options file
74
7
    return 0;
75
76
4.19k
  char *in = (char *) malloc(size + 1);
77
4.19k
  assert(in != NULL);
78
79
  // 0 terminate
80
0
  memcpy(in, data, size);
81
4.19k
  in[size] = 0;
82
83
  // the expression avoids removal of calls to pure functions
84
4.19k
  if (wget_iri_isreserved('='))
85
4.19k
    wget_iri_set_defaultpage("index.html");
86
87
4.19k
  test(in, size, "iso-8859-1");
88
4.19k
  test(in, size, "utf-8");
89
90
4.19k
  free(in);
91
92
4.19k
  return 0;
93
4.20k
}