Skip to content

Commit 1fe7cd3

Browse files
committed
exename: Set proper exename when cppcheck is executed from PATH and argv[0] does not contain path information
1 parent 18d24e2 commit 1fe7cd3

7 files changed

Lines changed: 28 additions & 46 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
937937
}
938938
}
939939

940-
mSettings->loadCppcheckCfg(argv[0]);
940+
mSettings->loadCppcheckCfg();
941941

942942
// Default template format..
943943
if (mSettings->templateFormat.empty()) {

cli/cppcheckexecutor.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
243243
int ret;
244244

245245
if (cppCheck.settings().exceptionHandling)
246-
ret = check_wrapper(cppCheck, argc, argv);
246+
ret = check_wrapper(cppCheck);
247247
else
248-
ret = check_internal(cppCheck, argc, argv);
248+
ret = check_internal(cppCheck);
249249

250250
mSettings = nullptr;
251251
return ret;
@@ -821,12 +821,12 @@ namespace {
821821
* TODO Check for multi-threading issues!
822822
*
823823
*/
824-
int CppCheckExecutor::check_wrapper(CppCheck& cppcheck, int argc, const char* const argv[])
824+
int CppCheckExecutor::check_wrapper(CppCheck& cppcheck)
825825
{
826826
#ifdef USE_WINDOWS_SEH
827827
FILE *outputFile = stdout;
828828
__try {
829-
return check_internal(cppcheck, argc, argv);
829+
return check_internal(cppcheck);
830830
} __except (filterException(GetExceptionCode(), GetExceptionInformation())) {
831831
// reporting to stdout may not be helpful within a GUI application...
832832
fputs("Please report this to the cppcheck developers!\n", outputFile);
@@ -854,23 +854,23 @@ int CppCheckExecutor::check_wrapper(CppCheck& cppcheck, int argc, const char* co
854854
for (std::map<int, std::string>::const_iterator sig=listofsignals.begin(); sig!=listofsignals.end(); ++sig) {
855855
sigaction(sig->first, &act, nullptr);
856856
}
857-
return check_internal(cppcheck, argc, argv);
857+
return check_internal(cppcheck);
858858
#else
859-
return check_internal(cppcheck, argc, argv);
859+
return check_internal(cppcheck);
860860
#endif
861861
}
862862

863863
/*
864864
* That is a method which gets called from check_wrapper
865865
* */
866-
int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const char* const argv[])
866+
int CppCheckExecutor::check_internal(CppCheck& cppcheck)
867867
{
868868
Settings& settings = cppcheck.settings();
869869
mSettings = &settings;
870-
const bool std = tryLoadLibrary(settings.library, argv[0], "std.cfg");
870+
const bool std = tryLoadLibrary(settings.library, settings.exename, "std.cfg");
871871

872872
for (const std::string &lib : settings.libraries) {
873-
if (!tryLoadLibrary(settings.library, argv[0], lib.c_str())) {
873+
if (!tryLoadLibrary(settings.library, settings.exename, lib.c_str())) {
874874
const std::string msg("Failed to load the library " + lib);
875875
const std::list<ErrorMessage::FileLocation> callstack;
876876
ErrorMessage errmsg(callstack, emptyString, Severity::information, msg, "failedToLoadCfg", Certainty::normal);
@@ -879,22 +879,15 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
879879
}
880880
}
881881

882-
bool posix = true;
883-
if (settings.posix())
884-
posix = tryLoadLibrary(settings.library, argv[0], "posix.cfg");
885-
bool windows = true;
886-
if (settings.isWindowsPlatform())
887-
windows = tryLoadLibrary(settings.library, argv[0], "windows.cfg");
888-
889-
if (!std || !posix || !windows) {
882+
if (!std) {
890883
const std::list<ErrorMessage::FileLocation> callstack;
891-
const std::string msg("Failed to load " + std::string(!std ? "std.cfg" : !posix ? "posix.cfg" : "windows.cfg") + ". Your Cppcheck installation is broken, please re-install.");
884+
const std::string msg("Failed to load std.cfg. Your Cppcheck installation is broken, please re-install.");
892885
#ifdef FILESDIR
893886
const std::string details("The Cppcheck binary was compiled with FILESDIR set to \""
894887
FILESDIR "\" and will therefore search for "
895888
"std.cfg in " FILESDIR "/cfg.");
896889
#else
897-
const std::string cfgfolder(Path::fromNativeSeparators(Path::getPathFromFilename(argv[0])) + "cfg");
890+
const std::string cfgfolder(Path::fromNativeSeparators(Path::getPathFromFilename(settings.exename)) + "cfg");
898891
const std::string details("The Cppcheck binary was compiled without FILESDIR set. Either the "
899892
"std.cfg should be available in " + cfgfolder + " or the FILESDIR "
900893
"should be configured.");
@@ -1143,9 +1136,9 @@ FILE* CppCheckExecutor::getExceptionOutput()
11431136
return mExceptionOutput;
11441137
}
11451138

1146-
bool CppCheckExecutor::tryLoadLibrary(Library& destination, const char* basepath, const char* filename)
1139+
bool CppCheckExecutor::tryLoadLibrary(Library& destination, const std::string& basepath, const char* filename)
11471140
{
1148-
const Library::Error err = destination.load(basepath, filename);
1141+
const Library::Error err = destination.load(basepath.c_str(), filename);
11491142

11501143
if (err.errorcode == Library::ErrorCode::UNKNOWN_ELEMENT)
11511144
std::cout << "cppcheck: Found unknown elements in configuration file '" << filename << "': " << err.reason << std::endl;

cli/cppcheckexecutor.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class CppCheckExecutor : public ErrorLogger {
111111
* Tries to load a library and prints warning/error messages
112112
* @return false, if an error occurred (except unknown XML elements)
113113
*/
114-
static bool tryLoadLibrary(Library& destination, const char* basepath, const char* filename);
114+
static bool tryLoadLibrary(Library& destination, const std::string& basepath, const char* filename);
115115

116116
/**
117117
* Execute a shell command and read the output from it. Returns true if command terminated successfully.
@@ -150,24 +150,20 @@ class CppCheckExecutor : public ErrorLogger {
150150
* - installs optional platform dependent signal handling
151151
*
152152
* @param cppcheck cppcheck instance
153-
* @param argc from main()
154-
* @param argv from main()
155153
**/
156-
int check_wrapper(CppCheck& cppcheck, int argc, const char* const argv[]);
154+
int check_wrapper(CppCheck& cppcheck);
157155

158156
/**
159157
* Starts the checking.
160158
*
161159
* @param cppcheck cppcheck instance
162-
* @param argc from main()
163-
* @param argv from main()
164160
* @return EXIT_FAILURE if arguments are invalid or no input files
165161
* were found.
166162
* If errors are found and --error-exitcode is used,
167163
* given value is returned instead of default 0.
168164
* If no errors are found, 0 is returned.
169165
*/
170-
int check_internal(CppCheck& cppcheck, int argc, const char* const argv[]);
166+
int check_internal(CppCheck& cppcheck);
171167

172168
/**
173169
* Pointer to current settings; set while check() is running.

gui/mainwindow.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,6 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLibrar
511511
checkSettings.checkLibrary = checkLibrary;
512512
checkSettings.checkConfiguration = checkConfiguration;
513513

514-
checkSettings.loadCppcheckCfg(QCoreApplication::applicationFilePath().toStdString());
515-
516514
if (mProjectFile)
517515
qDebug() << "Checking project file" << mProjectFile->getFilename();
518516

@@ -857,15 +855,10 @@ Settings MainWindow::getCppcheckSettings()
857855
result.exename = QCoreApplication::applicationFilePath().toStdString();
858856

859857
const bool std = tryLoadLibrary(&result.library, "std.cfg");
860-
bool posix = true;
861-
if (result.posix())
862-
posix = tryLoadLibrary(&result.library, "posix.cfg");
863-
bool windows = true;
864-
if (result.isWindowsPlatform())
865-
windows = tryLoadLibrary(&result.library, "windows.cfg");
866-
867-
if (!std || !posix || !windows)
868-
QMessageBox::critical(this, tr("Error"), tr("Failed to load %1. Your Cppcheck installation is broken. You can use --data-dir=<directory> at the command line to specify where this file is located. Please note that --data-dir is supposed to be used by installation scripts and therefore the GUI does not start when it is used, all that happens is that the setting is configured.").arg(!std ? "std.cfg" : !posix ? "posix.cfg" : "windows.cfg"));
858+
if (!std)
859+
QMessageBox::critical(this, tr("Error"), tr("Failed to load %1. Your Cppcheck installation is broken. You can use --data-dir=<directory> at the command line to specify where this file is located. Please note that --data-dir is supposed to be used by installation scripts and therefore the GUI does not start when it is used, all that happens is that the setting is configured.").arg("std.cfg"));
860+
861+
result.loadCppcheckCfg();
869862

870863
// If project file loaded, read settings from it
871864
if (mProjectFile) {

lib/settings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ Settings::Settings()
7878
certainty.setEnabled(Certainty::normal, true);
7979
}
8080

81-
void Settings::loadCppcheckCfg(const std::string &executable)
81+
void Settings::loadCppcheckCfg()
8282
{
83-
std::string fileName = Path::getPathFromFilename(executable) + "cppcheck.cfg";
83+
std::string fileName = Path::getPathFromFilename(exename) + "cppcheck.cfg";
8484
#ifdef FILESDIR
8585
if (Path::fileExists(FILESDIR "/cppcheck.cfg"))
8686
fileName = FILESDIR "/cppcheck.cfg";

lib/settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class CPPCHECKLIB Settings : public cppcheck::Platform {
9797
public:
9898
Settings();
9999

100-
void loadCppcheckCfg(const std::string &executable);
100+
void loadCppcheckCfg();
101101

102102
/** @brief addons, either filename of python/json file or json data */
103103
std::list<std::string> addons;

test/cfg/runtests.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ ${CPPCHECK} ${CPPCHECK_OPT} --inconclusive ${DIR}std.cpp
7070
# windows.cpp
7171
# Syntax check via g++ does not work because it can not find a valid windows.h
7272
#${CXX} ${CXX_OPT} ${DIR}windows.cpp
73-
${CPPCHECK} ${CPPCHECK_OPT} --inconclusive --platform=win32A ${DIR}windows.cpp
74-
${CPPCHECK} ${CPPCHECK_OPT} --inconclusive --platform=win32W ${DIR}windows.cpp
75-
${CPPCHECK} ${CPPCHECK_OPT} --inconclusive --platform=win64 ${DIR}windows.cpp
73+
${CPPCHECK} ${CPPCHECK_OPT} --inconclusive --platform=win32A --library=windows ${DIR}windows.cpp
74+
${CPPCHECK} ${CPPCHECK_OPT} --inconclusive --platform=win32W --library=windows ${DIR}windows.cpp
75+
${CPPCHECK} ${CPPCHECK_OPT} --inconclusive --platform=win64 --library=windows ${DIR}windows.cpp
7676

7777
# wxwidgets.cpp
7878
set +e

0 commit comments

Comments
 (0)