cmStringAlgorithmsFuzzer.cxx 2.1 KB

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