/src/target_parser_fuzzer.cpp
Line | Count | Source |
1 | | // Copyright 2026 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | //////////////////////////////////////////////////////////////////////////////// |
16 | | |
17 | | #include "systemd_target_parser.hpp" |
18 | | #include <fstream> |
19 | | #include <unistd.h> |
20 | | #include <vector> |
21 | | #include <string> |
22 | | #include <iostream> |
23 | | |
24 | | // Define the global variable required by systemd_target_parser |
25 | | bool gVerbose = false; |
26 | | |
27 | 4.44k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
28 | | // Write data to a temporary file |
29 | 4.44k | char temp_filename[] = "/tmp/target_parser_fuzzer_XXXXXX"; |
30 | 4.44k | int fd = mkstemp(temp_filename); |
31 | 4.44k | if (fd < 0) { |
32 | 0 | return 0; |
33 | 0 | } |
34 | | |
35 | 4.44k | if (write(fd, data, size) != static_cast<ssize_t>(size)) { |
36 | 0 | close(fd); |
37 | 0 | unlink(temp_filename); |
38 | 0 | return 0; |
39 | 0 | } |
40 | 4.44k | close(fd); |
41 | | |
42 | | // Call the function under test |
43 | 4.44k | try { |
44 | 4.44k | std::vector<std::string> filePaths = {temp_filename}; |
45 | 4.44k | parseFiles(filePaths); |
46 | 4.44k | } catch (const std::exception& e) { |
47 | | // We expect exceptions for invalid JSON or validation errors, |
48 | | // which is fine. We want to catch them so the fuzzer doesn't |
49 | | // treat them as crashes. |
50 | 4.16k | } |
51 | | |
52 | | // Clean up |
53 | 4.44k | unlink(temp_filename); |
54 | 4.44k | return 0; |
55 | 4.44k | } |