Coverage Report

Created: 2024-03-08 06:32

/src/wget2/fuzz/libwget_hsts_fuzzer.c
Line
Count
Source
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 <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 */
36
FILE *fopen(const char *pathname, const char *mode)
37
10.5k
{
38
10.5k
  FILE *(*libc_fopen)(const char *, const char *) =
39
10.5k
    (FILE *(*)(const char *, const char *)) dlsym (RTLD_NEXT, "fopen");
40
41
10.5k
  if (!strcmp(pathname, "hsts"))
42
5.20k
    return fmemopen((void *) g_data, g_size, mode);
43
44
5.29k
  return libc_fopen(pathname, mode);
45
10.5k
}
46
#endif
47
#endif
48
49
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
50
4.83k
{
51
4.83k
  if (size > 256) // same as max_len = 256 in .options file
52
56
    return 0;
53
54
4.77k
  g_data = data;
55
4.77k
  g_size = size;
56
57
4.77k
#if ! defined _WIN32 && defined HAVE_FMEMOPEN
58
4.77k
  wget_hsts_db *hsts_db = wget_hsts_db_init(NULL, "hsts");
59
4.77k
  wget_hsts_db_load(hsts_db);
60
#else
61
  wget_hsts_db *hsts_db = wget_hsts_db_init(NULL, NULL);
62
#endif
63
4.77k
  wget_hsts_host_match(hsts_db, "x.y", 80);
64
4.77k
  wget_hsts_db_free(&hsts_db);
65
66
4.77k
  return 0;
67
4.83k
}