/src/CMake/Tests/Fuzzing/cmStringAlgorithmsFuzzer.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 string algorithms |
6 | | * |
7 | | * Tests string manipulation, escaping, and processing functions. |
8 | | */ |
9 | | |
10 | | #include <cstddef> |
11 | | #include <cstdint> |
12 | | #include <string> |
13 | | #include <vector> |
14 | | |
15 | | #include "cmStringAlgorithms.h" |
16 | | #include "cmSystemTools.h" |
17 | | |
18 | | static constexpr size_t kMaxInputSize = 16 * 1024; |
19 | | |
20 | | extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) |
21 | 1.51k | { |
22 | 1.51k | if (size == 0 || size > kMaxInputSize) { |
23 | 14 | return 0; |
24 | 14 | } |
25 | | |
26 | 1.50k | std::string input(reinterpret_cast<char const*>(data), size); |
27 | | |
28 | | // Test string manipulation functions |
29 | 1.50k | (void)cmTrimWhitespace(input); |
30 | 1.50k | (void)cmRemoveQuotes(input); |
31 | 1.50k | (void)cmEscapeQuotes(input); |
32 | | |
33 | | // Test case conversion |
34 | 1.50k | (void)cmSystemTools::UpperCase(input); |
35 | 1.50k | (void)cmSystemTools::LowerCase(input); |
36 | | |
37 | | // Test tokenization |
38 | 1.50k | std::vector<std::string> tokens = cmTokenize(input, " \t\n\r"); |
39 | 1.50k | (void)tokens.size(); |
40 | | |
41 | | // Test with different separators if input is large enough |
42 | 1.50k | if (size > 4) { |
43 | 1.23k | std::string sep(reinterpret_cast<char const*>(data), 2); |
44 | 1.23k | std::string str(reinterpret_cast<char const*>(data + 2), size - 2); |
45 | 1.23k | std::vector<std::string> parts = cmTokenize(str, sep); |
46 | 1.23k | (void)parts.size(); |
47 | 1.23k | } |
48 | | |
49 | | // Test join operations |
50 | 1.50k | if (!tokens.empty()) { |
51 | 1.50k | (void)cmJoin(tokens, ";"); |
52 | 1.50k | (void)cmJoin(tokens, ","); |
53 | 1.50k | } |
54 | | |
55 | | // Test string contains |
56 | 1.50k | if (size > 2) { |
57 | 1.35k | std::string haystack(reinterpret_cast<char const*>(data), size / 2); |
58 | 1.35k | std::string needle(reinterpret_cast<char const*>(data + size / 2), |
59 | 1.35k | size - size / 2); |
60 | 1.35k | (void)cmHasPrefix(haystack, needle); |
61 | 1.35k | (void)cmHasSuffix(haystack, needle); |
62 | 1.35k | } |
63 | | |
64 | | // Test path operations |
65 | 1.50k | (void)cmSystemTools::GetFilenameWithoutExtension(input); |
66 | 1.50k | (void)cmSystemTools::GetFilenameExtension(input); |
67 | 1.50k | (void)cmSystemTools::GetFilenameName(input); |
68 | 1.50k | (void)cmSystemTools::GetFilenameLastExtension(input); |
69 | 1.50k | (void)cmSystemTools::GetFilenamePath(input); |
70 | | |
71 | | // Test path normalization |
72 | 1.50k | (void)cmSystemTools::CollapseFullPath(input); |
73 | | |
74 | 1.50k | return 0; |
75 | 1.51k | } |