/src/CMake/Tests/Fuzzing/cmGlobFuzzer.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | |
4 | | /* |
5 | | * Fuzzer for CMake's glob/regex matching |
6 | | * |
7 | | * Tests glob pattern matching and regex compilation. |
8 | | */ |
9 | | |
10 | | #include <cstddef> |
11 | | #include <cstdint> |
12 | | #include <string> |
13 | | #include <vector> |
14 | | |
15 | | #include "cmsys/Glob.hxx" |
16 | | #include "cmsys/RegularExpression.hxx" |
17 | | |
18 | | #include "cmSystemTools.h" |
19 | | |
20 | | static constexpr size_t kMaxInputSize = 4096; |
21 | | |
22 | | extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) |
23 | 2.04k | { |
24 | 2.04k | if (size == 0 || size > kMaxInputSize) { |
25 | 16 | return 0; |
26 | 16 | } |
27 | | |
28 | 2.03k | std::string input(reinterpret_cast<char const*>(data), size); |
29 | | |
30 | | // Test glob pattern matching |
31 | 2.03k | { |
32 | 2.03k | cmsys::Glob glob; |
33 | 2.03k | glob.SetRecurse(false); |
34 | 2.03k | glob.SetRelative("/tmp"); |
35 | | |
36 | | // Try to find files matching the pattern (safe - just pattern matching) |
37 | | // Don't actually recurse filesystem, just test pattern parsing |
38 | 2.03k | (void)glob.GetFiles(); |
39 | 2.03k | } |
40 | | |
41 | | // Test regex compilation (may throw on invalid patterns) |
42 | 2.03k | { |
43 | 2.03k | cmsys::RegularExpression regex; |
44 | 2.03k | bool compiled = regex.compile(input); |
45 | 2.03k | if (compiled) { |
46 | | // Test matching against some strings |
47 | 1.85k | (void)regex.find("test string"); |
48 | 1.85k | (void)regex.find(input); |
49 | 1.85k | (void)regex.find(""); |
50 | 1.85k | } |
51 | 2.03k | } |
52 | | |
53 | | // Test string matching utilities |
54 | 2.03k | (void)cmSystemTools::StringStartsWith(input, "CMAKE_"); |
55 | 2.03k | (void)cmSystemTools::StringEndsWith(input, ".cmake"); |
56 | | |
57 | | // Test simple pattern matching |
58 | 2.03k | if (size >= 4) { |
59 | 1.78k | std::string pattern(reinterpret_cast<char const*>(data), size / 2); |
60 | 1.78k | std::string text(reinterpret_cast<char const*>(data + size / 2), |
61 | 1.78k | size - size / 2); |
62 | | // Pattern matching is done through Glob::FindFiles, which we avoid |
63 | | // to prevent filesystem access. Just test string operations. |
64 | 1.78k | (void)pattern.length(); |
65 | 1.78k | (void)text.length(); |
66 | 1.78k | } |
67 | | |
68 | 2.03k | return 0; |
69 | 2.04k | } |