Skip to content

Commit

Permalink
Fixed #12440 (Misra violations found but code exited with 0 even afte…
Browse files Browse the repository at this point in the history
…r specifying exit code) (#6308)
  • Loading branch information
danmar committed Apr 19, 2024
1 parent 1fa728c commit 3601b18
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 22 deletions.
4 changes: 2 additions & 2 deletions cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ int CppCheckExecutor::check_internal(const Settings& settings) const
#endif
}

cppcheck.analyseWholeProgram(settings.buildDir, mFiles, mFileSettings);
returnValue |= cppcheck.analyseWholeProgram(settings.buildDir, mFiles, mFileSettings);

if (settings.severity.isEnabled(Severity::information) || settings.checkConfiguration) {
const bool err = reportSuppressions(settings, suppressions, settings.checks.isEnabled(Checks::unusedFunction), mFiles, mFileSettings, stdLogger);
Expand All @@ -311,7 +311,7 @@ int CppCheckExecutor::check_internal(const Settings& settings) const
stdLogger.reportErr(ErrorMessage::getXMLFooter());
}

if (settings.safety && stdLogger.hasCriticalErrors())
if (settings.safety && (stdLogger.hasCriticalErrors() || returnValue != 0))
return EXIT_FAILURE;

if (returnValue)
Expand Down
25 changes: 6 additions & 19 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@
#include <utility>
#include <vector>

#ifndef _WIN32
#include <unistd.h>
#else
#include <process.h>
#endif

#include "json.h"

#include <simplecpp.h>
Expand Down Expand Up @@ -139,15 +133,6 @@ static std::vector<std::string> split(const std::string &str, const std::string
return ret;
}

static int getPid()
{
#ifndef _WIN32
return getpid();
#else
return _getpid();
#endif
}

static std::string getDumpFileName(const Settings& settings, const std::string& filename)
{
if (!settings.dumpFile.empty())
Expand All @@ -157,7 +142,7 @@ static std::string getDumpFileName(const Settings& settings, const std::string&
if (settings.dump)
extension = ".dump";
else
extension = "." + std::to_string(getPid()) + ".dump";
extension = "." + std::to_string(settings.pid) + ".dump";

if (!settings.dump && !settings.buildDir.empty())
return AnalyzerInformation::getAnalyzerInfoFile(settings.buildDir, filename, emptyString) + extension;
Expand Down Expand Up @@ -1409,7 +1394,7 @@ void CppCheck::executeAddons(const std::vector<std::string>& files, const std::s
std::string fileList;

if (files.size() >= 2 || endsWith(files[0], ".ctu-info")) {
fileList = Path::getPathFromFilename(files[0]) + FILELIST + std::to_string(getPid());
fileList = Path::getPathFromFilename(files[0]) + FILELIST + std::to_string(mSettings.pid);
filesDeleter.addFile(fileList);
std::ofstream fout(fileList);
for (const std::string& f: files)
Expand Down Expand Up @@ -1770,12 +1755,12 @@ bool CppCheck::analyseWholeProgram()
return errors && (mExitCode > 0);
}

void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings)
unsigned int CppCheck::analyseWholeProgram(const std::string &buildDir, const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings)
{
executeAddonsWholeProgram(files); // TODO: pass FileSettings
if (buildDir.empty()) {
removeCtuInfoFiles(files, fileSettings);
return;
return mExitCode;
}
if (mSettings.checks.isEnabled(Checks::unusedFunction))
CheckUnusedFunctions::analyseWholeProgram(mSettings, *this, buildDir);
Expand Down Expand Up @@ -1836,6 +1821,8 @@ void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::list<

for (Check::FileInfo *fi : fileInfoList)
delete fi;

return mExitCode;
}

void CppCheck::removeCtuInfoFiles(const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings)
Expand Down
2 changes: 1 addition & 1 deletion lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
void analyseClangTidy(const FileSettings &fileSettings);

/** analyse whole program use .analyzeinfo files */
void analyseWholeProgram(const std::string &buildDir, const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings);
unsigned int analyseWholeProgram(const std::string &buildDir, const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings);

/** Remove *.ctu-info files */
void removeCtuInfoFiles(const std::list<std::pair<std::string, std::size_t>>& files, const std::list<FileSettings>& fileSettings); // cppcheck-suppress functionConst // has side effects
Expand Down
17 changes: 17 additions & 0 deletions lib/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@

#include "json.h"

#ifndef _WIN32
#include <unistd.h> // for getpid()
#else
#include <process.h> // for getpid()
#endif


std::atomic<bool> Settings::mTerminated;

const char Settings::SafeChecks::XmlRootName[] = "safe-checks";
Expand All @@ -37,12 +44,22 @@ const char Settings::SafeChecks::XmlExternalFunctions[] = "external-functions";
const char Settings::SafeChecks::XmlInternalFunctions[] = "internal-functions";
const char Settings::SafeChecks::XmlExternalVariables[] = "external-variables";

static int getPid()
{
#ifndef _WIN32
return getpid();
#else
return _getpid();
#endif
}

Settings::Settings()
{
severity.setEnabled(Severity::error, true);
certainty.setEnabled(Certainty::normal, true);
setCheckLevel(Settings::CheckLevel::exhaustive);
executor = defaultExecutor();
pid = getPid();
}

std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppressions)
Expand Down
3 changes: 3 additions & 0 deletions lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ class CPPCHECKLIB WARN_UNUSED Settings {
/** @brief max number of sets of arguments to pass to subfuncions in valueflow */
int performanceValueFlowMaxSubFunctionArgs = 256;

/** @brief pid of cppcheck. Intention is that this is set in the main process. */
int pid;

/** @brief plist output (--plist-output=&lt;dir&gt;) */
std::string plistOutput;

Expand Down
11 changes: 11 additions & 0 deletions test/cli/other_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ def test_internal_error(tmpdir):
assert stderr == '{}:0:0: error: Bailing from out analysis: Checking file failed: converting \'1f\' to integer failed - not an integer [internalError]\n\n^\n'.format(test_file)


def test_addon_ctu_exitcode(tmpdir):
""" #12440 - Misra ctu violations found => exit code should be non-zero """
test_file = os.path.join(tmpdir, 'test.c')
with open(test_file, 'wt') as f:
f.write("""typedef enum { BLOCK = 0x80U, } E;""")
args = ['--addon=misra', '--enable=style', '--error-exitcode=1', test_file]
exitcode, stdout, stderr = cppcheck(args)
assert '2.3' in stderr, stderr
assert exitcode == 1


# TODO: test with -j2
def test_addon_misra(tmpdir):
test_file = os.path.join(tmpdir, 'test.cpp')
Expand Down

0 comments on commit 3601b18

Please sign in to comment.