/src/CMake/Tests/Fuzzing/cmVersionFuzzer.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 version comparison |
6 | | * |
7 | | * Tests version string parsing and comparison operations. |
8 | | */ |
9 | | |
10 | | #include <cstddef> |
11 | | #include <cstdint> |
12 | | #include <string> |
13 | | |
14 | | #include "cmSystemTools.h" |
15 | | #include "cmVersion.h" |
16 | | |
17 | | static constexpr size_t kMaxInputSize = 1024; |
18 | | |
19 | | extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) |
20 | 357 | { |
21 | 357 | if (size == 0 || size > kMaxInputSize) { |
22 | 23 | return 0; |
23 | 23 | } |
24 | | |
25 | 334 | std::string input(reinterpret_cast<char const*>(data), size); |
26 | | |
27 | | // Test version comparison with current CMake version |
28 | 334 | (void)cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, input, "3.28.0"); |
29 | 334 | (void)cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER, input, |
30 | 334 | "3.28.0"); |
31 | 334 | (void)cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL, input, |
32 | 334 | "3.28.0"); |
33 | 334 | (void)cmSystemTools::VersionCompare(cmSystemTools::OP_LESS_EQUAL, input, |
34 | 334 | "3.28.0"); |
35 | 334 | (void)cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL, input, |
36 | 334 | "3.28.0"); |
37 | | |
38 | | // Compare against itself |
39 | 334 | (void)cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL, input, input); |
40 | | |
41 | | // If input is large enough, compare two different versions from input |
42 | 334 | if (size >= 4) { |
43 | 266 | std::string v1(reinterpret_cast<char const*>(data), size / 2); |
44 | 266 | std::string v2(reinterpret_cast<char const*>(data + size / 2), |
45 | 266 | size - size / 2); |
46 | 266 | (void)cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, v1, v2); |
47 | 266 | (void)cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER, v1, v2); |
48 | 266 | (void)cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL, v1, v2); |
49 | 266 | } |
50 | | |
51 | | // Version compare with greater/less string format |
52 | 334 | (void)cmSystemTools::VersionCompareGreater(input, "1.0.0"); |
53 | 334 | (void)cmSystemTools::VersionCompareGreaterEq(input, "1.0.0"); |
54 | | |
55 | 334 | return 0; |
56 | 357 | } |