File: cmListFileParserFuzzer.cxx

package info (click to toggle)
cmake 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 158,704 kB
  • sloc: ansic: 406,077; cpp: 309,512; sh: 4,233; python: 3,696; yacc: 3,109; lex: 1,279; f90: 538; asm: 471; lisp: 375; java: 310; cs: 270; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 110; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22; sed: 2
file content (84 lines) | stat: -rw-r--r-- 1,891 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file LICENSE.rst or https://cmake.org/licensing for details.  */

/*
 * Fuzzer for CMake's list file parser (CMakeLists.txt full parsing)
 *
 * This fuzzer tests the complete parsing of CMake files including:
 * - Command parsing
 * - Argument handling
 * - Bracket arguments
 * - Comments
 * - Line continuations
 */

#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <string>

#include <unistd.h>

#include "cmListFileCache.h"
#include "cmMessenger.h"
#include "cmSystemTools.h"

static constexpr size_t kMaxInputSize = 256 * 1024;
static std::string g_testDir;

extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
{
  (void)argc;
  (void)argv;

  char tmpl[] = "/tmp/cmake_fuzz_listfile_XXXXXX";
  char* dir = mkdtemp(tmpl);
  if (dir) {
    g_testDir = dir;
  } else {
    g_testDir = "/tmp/cmake_fuzz_listfile";
    cmSystemTools::MakeDirectory(g_testDir);
  }

  return 0;
}

extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
{
  if (size == 0 || size > kMaxInputSize) {
    return 0;
  }

  // Write input to temp file
  std::string testFile = g_testDir + "/CMakeLists.txt";
  {
    FILE* fp = fopen(testFile.c_str(), "wb");
    if (!fp)
      return 0;
    fwrite(data, 1, size, fp);
    fclose(fp);
  }

  // Create a messenger for error handling
  cmMessenger messenger;

  // Parse the file
  cmListFile listFile;
  cmListFileBacktrace backtrace;
  if (listFile.ParseFile(testFile.c_str(), &messenger, backtrace)) {
    // Successfully parsed - examine results
    for (auto const& func : listFile.Functions) {
      (void)func.LowerCaseName();
      (void)func.OriginalName();
      for (auto const& arg : func.Arguments()) {
        (void)arg.Value;
        (void)arg.Delim;
      }
    }
  }

  // Clean up
  unlink(testFile.c_str());

  return 0;
}