/src/unrar/unrar_fuzzer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include <stddef.h> |
2 | | #include <stdint.h> |
3 | | |
4 | | #include <filesystem> |
5 | | #include <fstream> |
6 | | #include <memory> |
7 | | #include <string> |
8 | | #include <system_error> |
9 | | #include <unistd.h> |
10 | | |
11 | | #include "rar.hpp" |
12 | | |
13 | | namespace fs = std::__fs::filesystem; |
14 | | |
15 | 4.09k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
16 | | // unrar likes to create files in the current directory. |
17 | | // So, the following few lines created and 'cd' to a directory named 'o' |
18 | | // in the current working directory. |
19 | 4.09k | std::error_code code, ok; |
20 | 4.09k | fs::path original_path = fs::current_path(code); |
21 | 4.09k | if (code != ok) return 0; |
22 | | |
23 | 4.09k | fs::path out_path = original_path / "o"; |
24 | 4.09k | bool created = fs::create_directory(out_path, code); |
25 | 4.09k | if (code != ok) return 0; |
26 | | |
27 | 4.09k | fs::current_path(out_path, code); |
28 | 4.09k | if (code != ok) return 0; |
29 | | |
30 | 4.09k | static const std::string filename = "temp.rar"; |
31 | 4.09k | std::ofstream file(filename, |
32 | 4.09k | std::ios::binary | std::ios::out | std::ios::trunc); |
33 | 4.09k | if (!file.is_open()) { |
34 | 0 | return 0; |
35 | 0 | } |
36 | 4.09k | file.write(reinterpret_cast<const char *>(data), size); |
37 | 4.09k | file.close(); |
38 | | |
39 | 4.09k | std::unique_ptr<CommandData> cmd_data(new CommandData); |
40 | 4.09k | cmd_data->ParseArg(const_cast<wchar_t *>(L"-p")); |
41 | 4.09k | cmd_data->ParseArg(const_cast<wchar_t *>(L"x")); |
42 | 4.09k | cmd_data->ParseDone(); |
43 | 4.09k | std::wstring wide_filename(filename.begin(), filename.end()); |
44 | 4.09k | cmd_data->AddArcName(wide_filename.c_str()); |
45 | | |
46 | 4.09k | try { |
47 | 4.09k | CmdExtract extractor(cmd_data.get()); |
48 | 4.09k | extractor.DoExtract(); |
49 | 4.09k | } catch (...) { |
50 | 131 | } |
51 | | |
52 | 4.09k | unlink(filename.c_str()); |
53 | | |
54 | | // 'cd' back to the original directory and delete 'o' along with |
55 | | // all its contents. |
56 | 4.09k | fs::current_path(original_path, code); |
57 | 4.09k | if (code != ok) return 0; |
58 | 4.09k | fs::remove_all(out_path, code); |
59 | 4.09k | if (code != ok) return 0; |
60 | 4.09k | return 0; |
61 | 4.09k | } |