Debian Codegraph @

fuzzing/apps/db_dump/db_dump_fuzz_load.cpp

raw
1 /*
2 Copyright (c) 2023 Cedalo GmbH
3
4 All rights reserved. This program and the accompanying materials
5 are made available under the terms of the Eclipse Public License 2.0
6 and Eclipse Distribution License v1.0 which accompany this distribution.
7
8 The Eclipse Public License is available at
9 https://www.eclipse.org/legal/epl-2.0/
10 and the Eclipse Distribution License is available at
11 http://www.eclipse.org/org/documents/edl-v10.php.
12
13 SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14
15 Contributors:
16 Roger Light - initial implementation and documentation.
17 */
18
19 #include <cstdio>
20 #include <cstdint>
21 #include <cstdlib>
22 #include <cstring>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 /*
27 * Test loading a file
28 */
29
30
31 /* The fuzz-only main function. */
32 extern "C" int db_dump_fuzz_main(int argc, char *argv[]);
33
34
35 void run_db_dump(char *filename)
36 {
37 char *argv[2];
38 int argc = 2;
39
40 argv[0] = strdup("mosquitto_db_dump");
41 argv[1] = filename;
42
43 db_dump_fuzz_main(argc, argv);
44
45 free(argv[0]);
46 }
47
48
49 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
50 {
51 char filename[100];
52 FILE *fptr;
53
54 umask(0077);
55
56 snprintf(filename, sizeof(filename), "db_dump_%d.db", getpid());
57 fptr = fopen(filename, "wb");
58 if(!fptr){
59 return 1;
60 }
61 fwrite(data, 1, size, fptr);
62 fclose(fptr);
63
64 run_db_dump(filename);
65
66 unlink(filename);
67
68 return 0;
69 }