Debian Codegraph @

tests/ossfuzz/standaloneengine.cc

raw
1 /* Copyright (C) The libssh2 project and its contributors.
2 *
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include "testinput.h"
9
10 /**
11 * Main procedure for standalone fuzzing engine.
12 *
13 * Reads filenames from the argument array. For each filename, read the file
14 * into memory and then call the fuzzing interface with the data.
15 */
16 int main(int argc, char **argv)
17 {
18 int ii;
19 for(ii = 1; ii < argc; ii++)
20 {
21 FILE *infile;
22 printf("[%s] ", argv[ii]);
23
24 /* Try and open the file. */
25 infile = fopen(argv[ii], "rb");
26 if(infile)
27 {
28 uint8_t *buffer = NULL;
29 size_t buffer_len;
30
31 printf("Opened.. ");
32
33 /* Get the length of the file. */
34 fseek(infile, 0L, SEEK_END);
35 buffer_len = ftell(infile);
36
37 /* Reset the file indicator to the beginning of the file. */
38 fseek(infile, 0L, SEEK_SET);
39
40 /* Allocate a buffer for the file contents. */
41 buffer = (uint8_t *)calloc(buffer_len, sizeof(uint8_t));
42 if(buffer)
43 {
44 /* Read all the text from the file into the buffer. */
45 fread(buffer, sizeof(uint8_t), buffer_len, infile);
46 printf("Read %zu bytes, fuzzing.. ", buffer_len);
47
48 /* Call the fuzzer with the data. */
49 LLVMFuzzerTestOneInput(buffer, buffer_len);
50
51 printf("complete.");
52
53 /* Free the buffer as it's no longer needed. */
54 free(buffer);
55 buffer = NULL;
56 }
57 else
58 {
59 fprintf(stderr,
60 "[%s] Failed to allocate %zu bytes \n",
61 argv[ii],
62 buffer_len);
63 }
64
65 /* Close the file as it's no longer needed. */
66 fclose(infile);
67 infile = NULL;
68 }
69 else
70 {
71 /* Failed to open the file. Maybe wrong name or wrong permissions? */
72 fprintf(stderr, "[%s] Open failed. \n", argv[ii]);
73 }
74
75 printf("\n");
76 }
77 }