Skip to content

Commit 2fd0bc7

Browse files
catenacyberfenner
authored andcommitted
Portability on windows for fuzzing
1 parent 4615fef commit 2fd0bc7

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

fuzz/fuzz_pcap.c

+20-13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ void fuzz_openFile(const char * name) {
3434
outfile = fopen(name, "w");
3535
}
3636

37+
static int bufferToFile(const char * name, const uint8_t *Data, size_t Size) {
38+
FILE * fd;
39+
if (remove(name) != 0) {
40+
printf("failed remove, errno=%d\n", errno);
41+
return -1;
42+
}
43+
fd = fopen(name, "wb");
44+
if (fd == NULL) {
45+
printf("failed open, errno=%d\n", errno);
46+
return -2;
47+
}
48+
if (fwrite (Data, 1, Size, fd) != Size) {
49+
fclose(fd);
50+
return -3;
51+
}
52+
fclose(fd);
53+
return 0;
54+
}
55+
3756
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
3857
pcap_t * pkts;
3958
char errbuf[PCAP_ERRBUF_SIZE];
@@ -63,21 +82,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
6382
init_print(&Ndo, 0, 0);
6483

6584
//rewrite buffer to a file as libpcap does not have buffer inputs
66-
int fd = open("/tmp/fuzz.pcap", O_RDWR | O_CREAT, 0666);
67-
if (fd == -1) {
68-
printf("failed open, errno=%d\n", errno);
69-
return 0;
70-
}
71-
if (ftruncate(fd, Size) == -1) {
72-
return 0;
73-
}
74-
if (lseek (fd, 0, SEEK_SET) < 0) {
75-
return 0;
76-
}
77-
if (write (fd, Data, Size) != Size) {
85+
if (bufferToFile("/tmp/fuzz.pcap", Data, Size) < 0) {
7886
return 0;
7987
}
80-
close(fd);
8188

8289
//initialize structure
8390
pkts = pcap_open_offline("/tmp/fuzz.pcap", errbuf);

0 commit comments

Comments
 (0)