Coverage Report

Created: 2025-08-26 06:08

/src/buffer_add_file_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2023 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
      http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
13
#include <stddef.h>
14
#include <stdint.h>
15
#include <stdlib.h>
16
#include <string>
17
#include <sys/socket.h>
18
#include <sys/stat.h>
19
#include <unistd.h>
20
21
#include <fuzzer/FuzzedDataProvider.h>
22
23
extern "C" {
24
#include "libevent/include/event2/buffer.h"
25
#include "libevent/include/event2/buffer_compat.h"
26
#include "libevent/include/event2/event.h"
27
#include "libevent/include/event2/util.h"
28
#include "util-internal.h"
29
}
30
31
206
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
32
206
  FuzzedDataProvider data_provider(data, size);
33
34
206
  std::string s1 = data_provider.ConsumeRandomLengthString();
35
206
  uint32_t int1 = data_provider.ConsumeIntegral<uint32_t>();
36
37
206
  char bufferFile[50];
38
206
  struct stat st;
39
40
206
  sprintf(bufferFile, "/tmp/buffer.%d", getpid());
41
206
  FILE *fp = fopen(bufferFile, "wb");
42
206
  if (!fp) {
43
0
    return 0;
44
0
  }
45
206
  fwrite(s1.c_str(), s1.size(), 1, fp);
46
206
  fclose(fp);
47
48
206
  fp = fopen(bufferFile, "rb");
49
206
  if (!fp) {
50
0
    return 0;
51
0
  }
52
53
206
  int fd = fileno(fp);
54
206
  fstat(fd, &st);
55
56
206
  struct evbuffer *buf = evbuffer_new();
57
206
  evbuffer_set_flags(buf, int1);
58
206
  evbuffer_add_file(buf, fd, 0, st.st_size);
59
60
206
  fclose(fp);
61
206
  close(fd);
62
63
206
  unlink(bufferFile);
64
206
  evbuffer_free(buf);
65
206
  return 0;
66
206
}