|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2026 Cppcheck team. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +// Fuzzing rationale (oss-fuzz-client-cli): |
| 20 | +// |
| 21 | +// The entire cli/ front end (cli/cmdlineparser.cpp alone is ~2.3k lines) is |
| 22 | +// not part of the OSS-Fuzz build at all: the existing harness links only |
| 23 | +// lib/ + the grammar generator, so the command-line parser - which parses |
| 24 | +// fully attacker-controlled text (users routinely run cppcheck on untrusted |
| 25 | +// repositories whose build scripts embed crafted arguments, and CI systems |
| 26 | +// pass user-controlled option strings) - has zero coverage by construction. |
| 27 | +// |
| 28 | +// This harness feeds NUL-separated argument strings through the same public |
| 29 | +// entry point the executor uses (CmdLineParser::fillSettingsFromArgs), |
| 30 | +// which returns a Result instead of exiting, and probes the parsed file |
| 31 | +// lists. Options such as --library/--project/--suppressions-list/--rule |
| 32 | +// additionally drive the library/project/suppression loading code paths |
| 33 | +// with attacker-controlled names. Findings here are real bugs reachable |
| 34 | +// from the command line. |
| 35 | +// |
| 36 | +// Input format: NUL-separated argument strings (argv[0] "cppcheck" is |
| 37 | +// synthesized by the harness; the input holds only the arguments). |
| 38 | + |
| 39 | +#include "cmdlineparser.h" |
| 40 | +#include "cmdlinelogger.h" |
| 41 | +#include "settings.h" |
| 42 | +#include "suppressions.h" |
| 43 | + |
| 44 | +#include <cstddef> |
| 45 | +#include <cstdint> |
| 46 | +#include <string> |
| 47 | +#include <vector> |
| 48 | + |
| 49 | +namespace { |
| 50 | + // argument strings are short; keep runtime bounded |
| 51 | + constexpr size_t kMaxInputSize = 4096; |
| 52 | + constexpr int kMaxArgs = 64; |
| 53 | + |
| 54 | + class NullLogger : public CmdLineLogger { |
| 55 | + public: |
| 56 | + void printMessage(const std::string&) override {} |
| 57 | + void printError(const std::string&) override {} |
| 58 | + void printRaw(const std::string&) override {} |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize); |
| 63 | + |
| 64 | +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize) |
| 65 | +{ |
| 66 | + if (dataSize < 2 || dataSize > kMaxInputSize) |
| 67 | + return 0; |
| 68 | + |
| 69 | + std::vector<std::string> args; |
| 70 | + args.push_back("cppcheck"); |
| 71 | + { |
| 72 | + size_t start = 0; |
| 73 | + for (size_t i = 0; i <= dataSize && args.size() < kMaxArgs; ++i) { |
| 74 | + if (i == dataSize || data[i] == 0) { |
| 75 | + if (i > start) { |
| 76 | + const size_t len = (i - start) > 1024 ? 1024 : (i - start); |
| 77 | + args.push_back(std::string(reinterpret_cast<const char*>(data) + start, len)); |
| 78 | + } |
| 79 | + start = i + 1; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + std::vector<const char*> argv; |
| 85 | + argv.reserve(args.size()); |
| 86 | + for (const std::string& a : args) |
| 87 | + argv.push_back(a.c_str()); |
| 88 | + |
| 89 | + try { |
| 90 | + Settings settings; |
| 91 | + Suppressions supprs; |
| 92 | + NullLogger logger; |
| 93 | + CmdLineParser parser(logger, settings, supprs); |
| 94 | + (void)parser.fillSettingsFromArgs(static_cast<int>(argv.size()), argv.data()); |
| 95 | + // probe the parse results cheaply |
| 96 | + (void)parser.getFiles().size(); |
| 97 | + (void)parser.getFileSettings().size(); |
| 98 | + } catch (const std::exception&) { |
| 99 | + // options such as --rule=... may throw on bad regexes; a fuzzer must not die |
| 100 | + } catch (...) { |
| 101 | + } |
| 102 | + return 0; |
| 103 | +} |
0 commit comments