Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions cli/threadexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ void ThreadExecutor::reportInternalChildErr(const std::string &childname, const

#elif defined(THREADING_MODEL_THREAD)

class ThreadExecutor::LogWriter : public ErrorLogger
class ThreadExecutor::SyncLogForwarder : public ErrorLogger
{
public:
LogWriter(ThreadExecutor &threadExecutor)
SyncLogForwarder(ThreadExecutor &threadExecutor)
: mThreadExecutor(threadExecutor), mProcessedFiles(0), mTotalFiles(0), mProcessedSize(0), mTotalFileSize(0) {

mItNextFile = threadExecutor.mFiles.begin();
Expand Down Expand Up @@ -475,11 +475,11 @@ unsigned int ThreadExecutor::check()
std::vector<std::future<unsigned int>> threadFutures;
threadFutures.reserve(mSettings.jobs);

LogWriter logwriter(*this);
SyncLogForwarder logforwarder(*this);

for (unsigned int i = 0; i < mSettings.jobs; ++i) {
try {
threadFutures.emplace_back(std::async(std::launch::async, threadProc, &logwriter));
threadFutures.emplace_back(std::async(std::launch::async, threadProc, &logforwarder));
}
catch (const std::system_error &e) {
std::cerr << "#### ThreadExecutor::check exception :" << e.what() << std::endl;
Expand All @@ -492,51 +492,51 @@ unsigned int ThreadExecutor::check()
});
}

unsigned int STDCALL ThreadExecutor::threadProc(LogWriter* logWriter)
unsigned int STDCALL ThreadExecutor::threadProc(SyncLogForwarder* logForwarder)
{
unsigned int result = 0;

std::map<std::string, std::size_t>::const_iterator &itFile = logWriter->mItNextFile;
std::list<ImportProject::FileSettings>::const_iterator &itFileSettings = logWriter->mItNextFileSettings;
std::map<std::string, std::size_t>::const_iterator &itFile = logForwarder->mItNextFile;
std::list<ImportProject::FileSettings>::const_iterator &itFileSettings = logForwarder->mItNextFileSettings;

// guard static members of CppCheck against concurrent access
logWriter->mFileSync.lock();
logForwarder->mFileSync.lock();

for (;;) {
if (itFile == logWriter->mThreadExecutor.mFiles.end() && itFileSettings == logWriter->mThreadExecutor.mSettings.project.fileSettings.end()) {
logWriter->mFileSync.unlock();
if (itFile == logForwarder->mThreadExecutor.mFiles.end() && itFileSettings == logForwarder->mThreadExecutor.mSettings.project.fileSettings.end()) {
logForwarder->mFileSync.unlock();
break;
}

CppCheck fileChecker(*logWriter, false, CppCheckExecutor::executeCommand);
fileChecker.settings() = logWriter->mThreadExecutor.mSettings;
CppCheck fileChecker(*logForwarder, false, CppCheckExecutor::executeCommand);
fileChecker.settings() = logForwarder->mThreadExecutor.mSettings;

std::size_t fileSize = 0;
if (itFile != logWriter->mThreadExecutor.mFiles.end()) {
if (itFile != logForwarder->mThreadExecutor.mFiles.end()) {
const std::string &file = itFile->first;
fileSize = itFile->second;
++itFile;

logWriter->mFileSync.unlock();
logForwarder->mFileSync.unlock();

// Read file from a file
result += fileChecker.check(file);
} else { // file settings..
const ImportProject::FileSettings &fs = *itFileSettings;
++itFileSettings;
logWriter->mFileSync.unlock();
logForwarder->mFileSync.unlock();
result += fileChecker.check(fs);
if (logWriter->mThreadExecutor.mSettings.clangTidy)
if (logForwarder->mThreadExecutor.mSettings.clangTidy)
fileChecker.analyseClangTidy(fs);
}

logWriter->mFileSync.lock();
logForwarder->mFileSync.lock();

logWriter->mProcessedSize += fileSize;
logWriter->mProcessedFiles++;
if (!logWriter->mThreadExecutor.mSettings.quiet) {
std::lock_guard<std::mutex> lg(logWriter->mReportSync);
CppCheckExecutor::reportStatus(logWriter->mProcessedFiles, logWriter->mTotalFiles, logWriter->mProcessedSize, logWriter->mTotalFileSize);
logForwarder->mProcessedSize += fileSize;
logForwarder->mProcessedFiles++;
if (!logForwarder->mThreadExecutor.mSettings.quiet) {
std::lock_guard<std::mutex> lg(logForwarder->mReportSync);
CppCheckExecutor::reportStatus(logForwarder->mProcessedFiles, logForwarder->mTotalFiles, logForwarder->mProcessedSize, logForwarder->mTotalFileSize);
}
}
return result;
Expand Down
4 changes: 2 additions & 2 deletions cli/threadexecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class ThreadExecutor {

#elif defined(THREADING_MODEL_THREAD)

class LogWriter;
static unsigned int STDCALL threadProc(LogWriter *threadExecutor);
class SyncLogForwarder;
static unsigned int STDCALL threadProc(SyncLogForwarder *logforwarder);

#endif

Expand Down
47 changes: 12 additions & 35 deletions oss-fuzz/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,30 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "color.h"
#include "cppcheck.h"
#include "type2.h"

enum class Color;

class CppcheckExecutor : public ErrorLogger {
private:
CppCheck cppcheck;

class DummyErrorLogger : public ErrorLogger {
public:
CppcheckExecutor()
: ErrorLogger()
, cppcheck(*this, false, nullptr) {
cppcheck.settings().addEnabled("all");
cppcheck.settings().certainty.setEnabled(Certainty::inconclusive, true);
}

void run(const std::string &code) {
cppcheck.check("test.cpp", code);
}

void reportOut(const std::string &outmsg, Color) override {
(void)outmsg;
}
void reportErr(const ErrorMessage &msg) override {
(void)msg;
}
void reportProgress(const std::string& filename,
const char stage[],
const std::size_t value) override {
(void)filename;
(void)stage;
(void)value;
}
void reportOut(const std::string&, Color) override {}
void reportErr(const ErrorMessage&) override {}
void reportProgress(const std::string&,
const char[],
const std::size_t) override {}
};


extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize)
{
if (dataSize < 10000) {
const std::string code = generateCode2(data, dataSize);
//std::ofstream fout("code.cpp");
//fout << code;
//fout.close();

CppcheckExecutor cppcheckExecutor;
cppcheckExecutor.run(code);
DummyErrorLogger errorLogger;
CppCheck cppcheck(errorLogger, false, nullptr);
cppcheck.settings().addEnabled("all");
cppcheck.settings().certainty.setEnabled(Certainty::inconclusive, true);
cppcheck.check("test.cpp", code);
}
return 0;
}
Expand Down
17 changes: 17 additions & 0 deletions oss-fuzz/translate.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2022 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include <fstream>
Expand Down
19 changes: 17 additions & 2 deletions oss-fuzz/type2.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2022 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <sstream>
#include "type2.h"



static int getValue(const uint8_t *data, size_t dataSize, uint8_t maxValue, bool *done = nullptr)
{
static size_t pos; // current "data" position
Expand Down
17 changes: 17 additions & 0 deletions oss-fuzz/type2.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2022 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

Expand Down