Coverage Report

Created: 2025-08-29 06:36

/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
204
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
32
204
  FuzzedDataProvider data_provider(data, size);
33
34
204
  std::string s1 = data_provider.ConsumeRandomLengthString();
35
204
  uint32_t int1 = data_provider.ConsumeIntegral<uint32_t>();
36
37
204
  char bufferFile[50];
38
204
  struct stat st;
39
40
204
  sprintf(bufferFile, "/tmp/buffer.%d", getpid());
41
204
  FILE *fp = fopen(bufferFile, "wb");
42
204
  if (!fp) {
43
0
    return 0;
44
0
  }
45
204
  fwrite(s1.c_str(), s1.size(), 1, fp);
46
204
  fclose(fp);
47
48
204
  fp = fopen(bufferFile, "rb");
49
204
  if (!fp) {
50
0
    return 0;
51
0
  }
52
53
204
  int fd = fileno(fp);
54
204
  fstat(fd, &st);
55
56
204
  struct evbuffer *buf = evbuffer_new();
57
204
  evbuffer_set_flags(buf, int1);
58
204
  evbuffer_add_file(buf, fd, 0, st.st_size);
59
60
204
  fclose(fp);
61
204
  close(fd);
62
63
204
  unlink(bufferFile);
64
204
  evbuffer_free(buf);
65
204
  return 0;
66
204
}