|
| 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-raw): |
| 20 | +// |
| 21 | +// The existing OSS-Fuzz harness (main.cpp) rewrites every fuzzer input through |
| 22 | +// the type2 grammar generator before analysis, and that grammar never emits |
| 23 | +// preprocessor directives (#include/#define/#if/...), raw string literals, |
| 24 | +// line markers or many other C/C++ constructs. The preprocessor |
| 25 | +// (lib/preprocessor.cpp + externals/simplecpp) and the diagnostic-formatting |
| 26 | +// layer (lib/errorlogger.cpp) are therefore structurally unreachable for the |
| 27 | +// mutator even though they parse fully attacker-controlled text in production |
| 28 | +// (cppcheck is routinely run on untrusted source files by CI systems). |
| 29 | +// |
| 30 | +// This harness feeds the raw bytes straight into the same analysis entry |
| 31 | +// point (CppCheck::checkBuffer) so the lexer, preprocessor and error |
| 32 | +// formatting layers are exercised with unmodified attacker-controlled text. |
| 33 | +// A memory-safety or UB finding here is a real bug reachable by anyone |
| 34 | +// running cppcheck on untrusted code. |
| 35 | +// |
| 36 | +// Input format: byte 0 selects the language mode (odd bit: C++, else C); |
| 37 | +// bytes 1.. are the source text passed through verbatim. |
| 38 | + |
| 39 | +#include "cppcheck.h" |
| 40 | +#include "errorlogger.h" |
| 41 | +#include "errortypes.h" |
| 42 | +#include "filesettings.h" |
| 43 | +#include "settings.h" |
| 44 | +#include "standards.h" |
| 45 | +#include "suppressions.h" |
| 46 | + |
| 47 | +#include <cstddef> |
| 48 | +#include <cstdint> |
| 49 | +#include <string> |
| 50 | + |
| 51 | +namespace { |
| 52 | + class DummyErrorLogger : public ErrorLogger { |
| 53 | + public: |
| 54 | + void reportOut(const std::string& /*outmsg*/, Color /*c*/) override {} |
| 55 | + void reportErr(const ErrorMessage& /*msg*/) override {} |
| 56 | + void reportProgress(const std::string& /*filename*/, |
| 57 | + const char /*stage*/[], |
| 58 | + const std::size_t /*value*/) override {} |
| 59 | + void reportMetric(const std::string & /*metric*/) override {} |
| 60 | + }; |
| 61 | + |
| 62 | + Settings create_settings() |
| 63 | + { |
| 64 | + Settings s; |
| 65 | + s.templateFormat = "{bold}{file}:{line}:{column}: {red}{inconclusive:{magenta}}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}"; |
| 66 | + s.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}"; |
| 67 | + s.addEnabled("all"); |
| 68 | + s.certainty.setEnabled(Certainty::inconclusive, true); |
| 69 | + return s; |
| 70 | + } |
| 71 | +} // namespace |
| 72 | + |
| 73 | +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize); |
| 74 | + |
| 75 | +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize) |
| 76 | +{ |
| 77 | + // keep analysis cost bounded; mirrors the main.cpp input cap |
| 78 | + if (dataSize < 2 || dataSize >= 10000) |
| 79 | + return 0; |
| 80 | + |
| 81 | + const bool cppSource = (data[0] & 1) != 0; |
| 82 | + const std::string code(reinterpret_cast<const char*>(data + 1), dataSize - 1); |
| 83 | + |
| 84 | + static DummyErrorLogger s_errorLogger; |
| 85 | + Suppressions supprs; |
| 86 | + const Settings s_settings = create_settings(); |
| 87 | + CppCheck cppcheck(s_settings, supprs, s_errorLogger, nullptr, false, nullptr); |
| 88 | + const FileWithDetails file(cppSource ? "test.cpp" : "test.c", |
| 89 | + cppSource ? Standards::Language::CPP : Standards::Language::C, |
| 90 | + code.size()); |
| 91 | + cppcheck.checkBuffer(file, code.data(), code.size()); |
| 92 | + return 0; |
| 93 | +} |
0 commit comments