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