@@ -34,6 +34,25 @@ void fuzz_openFile(const char * name) {
34
34
outfile = fopen (name , "w" );
35
35
}
36
36
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
+
37
56
int LLVMFuzzerTestOneInput (const uint8_t * Data , size_t Size ) {
38
57
pcap_t * pkts ;
39
58
char errbuf [PCAP_ERRBUF_SIZE ];
@@ -63,21 +82,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
63
82
init_print (& Ndo , 0 , 0 );
64
83
65
84
//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 ) {
78
86
return 0 ;
79
87
}
80
- close (fd );
81
88
82
89
//initialize structure
83
90
pkts = pcap_open_offline ("/tmp/fuzz.pcap" , errbuf );
0 commit comments