Coverage Report

Created: 2025-11-16 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wget2/fuzz/wget_options_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 <sys/types.h>
23
#include <dirent.h> // opendir, readdir
24
#include <stdint.h> // uint8_t
25
#include <stdio.h>  // fmemopen
26
#include <string.h>  // strncmp
27
28
#include "wget.h"
29
30
#ifdef  __cplusplus
31
  extern "C" {
32
#endif
33
#include "../src/wget_options.h"
34
#include "../src/wget_plugin.h"
35
#include "../src/wget_testing.h"
36
#ifdef __cplusplus
37
  }
38
#endif
39
40
#include "fuzzer.h"
41
42
static const uint8_t *g_data;
43
static size_t g_size;
44
45
#if defined HAVE_DLFCN_H && defined HAVE_FMEMOPEN
46
#include <dlfcn.h>
47
#ifdef RTLD_NEXT /* Not defined e.g. on CygWin */
48
DIR *opendir(const char *name)
49
623
{
50
623
  DIR *(*libc_opendir)(const char *) =
51
623
    (DIR *(*)(const char *)) dlsym (RTLD_NEXT, "opendir");
52
53
623
  if (config.dont_write)
54
620
    return NULL;
55
56
3
  return libc_opendir(name);
57
/*
58
#ifdef TEST_RUN
59
  printf("opendir %s\n", name);
60
  if (!strcmp(name, SRCDIR"/wget_options_fuzzer.in"))
61
    return libc_opendir(name);
62
  if (!strcmp(name, SRCDIR"/wget_options_fuzzer.new"))
63
    return libc_opendir(name);
64
  if (!strcmp(name, SRCDIR"/wget_options_fuzzer.repro"))
65
    return libc_opendir(name);
66
#else
67
  if (!strcmp(name, "wget_options_fuzzer.in"))
68
    return libc_opendir(name);
69
  if (!strcmp(name, "wget_options_fuzzer.new"))
70
    return libc_opendir(name);
71
  if (!strcmp(name, "wget_options_fuzzer.repro"))
72
    return libc_opendir(name);
73
#endif
74
75
  return libc_opendir(name);
76
*/
77
623
}
78
79
FILE *fopen(const char *pathname, const char *mode)
80
41.5k
{
81
41.5k
  FILE *(*libc_fopen)(const char *, const char *) =
82
41.5k
    (FILE *(*)(const char *, const char *)) dlsym (RTLD_NEXT, "fopen");
83
84
41.5k
  if (config.dont_write) {
85
31.4k
    if (!strcmp(pathname, "d41d8cd98f00b204e9800998ecf8428e") && !strcmp(mode, "r"))
86
24.0k
      return fmemopen((void *) g_data, g_size, mode);
87
88
//    printf("open %s, %s\n", pathname, mode);
89
31.4k
  }
90
91
17.5k
  return libc_fopen(pathname, mode);
92
41.5k
}
93
#endif
94
#endif
95
96
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
97
11.2k
{
98
11.2k
  if (size > 2048) // same as max_len = 4096 in .options file
99
15
    return 0;
100
101
11.2k
  g_data = data;
102
11.2k
  g_size = size;
103
104
11.2k
  config.dont_write = 1;
105
106
11.2k
  if (size == 0)
107
103
    selftest_options();
108
109
// try not to open/write to the file system
110
11.2k
#if defined HAVE_DLFCN_H && defined HAVE_FMEMOPEN
111
11.2k
  static const char *argv[] = { "x", "-q", "--no-config", "--no-local-db", "--config", "d41d8cd98f00b204e9800998ecf8428e" };
112
11.2k
  plugin_db_init();
113
11.2k
  enable_testing(); // function in wget2 to prevent unwanted action while testing
114
11.2k
  init(sizeof(argv)/sizeof(argv[0]), argv);
115
11.2k
  deinit();
116
11.2k
  plugin_db_finalize(0);
117
11.2k
#endif
118
119
11.2k
  config.dont_write = 0;
120
121
11.2k
  return 0;
122
11.2k
}