From 9f054fbb3c2dd707e3a46f6e93007a045414be06 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sat, 14 Apr 2012 14:44:15 +0200 Subject: [PATCH] Fixed crash when "-rp=" or "--relative-paths=" is given - print Error. --- cli/cmdlineparser.cpp | 19 ++++++++++++------- lib/path.cpp | 2 +- test/testpath.cpp | 1 + 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 202a9a4d4..00724a8b1 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -254,13 +254,18 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) _settings->_relativePaths = true; else if (strncmp(argv[i], "-rp=", 4) == 0 || strncmp(argv[i], "--relative-paths=", 17) == 0) { _settings->_relativePaths = true; - std::string paths = argv[i]+(argv[i][3]=='='?4:17); - std::string::size_type pos; - do { - pos = paths.find(';'); - _settings->_basePaths.push_back(Path::fromNativeSeparators(paths.substr(0, pos))); - paths.erase(0, pos+1); - } while (pos != std::string::npos); + if (argv[i][argv[i][3]=='='?4:17] != 0) { + std::string paths = argv[i]+(argv[i][3]=='='?4:17); + std::string::size_type pos; + do { + pos = paths.find(';'); + _settings->_basePaths.push_back(Path::fromNativeSeparators(paths.substr(0, pos))); + paths.erase(0, pos+1); + } while (pos != std::string::npos); + } else { + PrintMessage("cppcheck: No paths specified for the '" + std::string(argv[i]) + "' option."); + return false; + } } // Write results in results.xml diff --git a/lib/path.cpp b/lib/path.cpp index 20122fd5a..425bdc28f 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -157,7 +157,7 @@ std::string Path::getFilenameExtensionInLowerCase(const std::string &path) std::string Path::getRelativePath(const std::string& absolutePath, const std::vector& basePaths) { for (std::vector::const_iterator i = basePaths.begin(); i != basePaths.end(); ++i) { - if (absolutePath == *i) // Seems to be a file + if (absolutePath == *i || i->empty()) // Seems to be a file, or path is empty continue; bool endsWithSep = (*i)[i->length()-1] == '/'; diff --git a/test/testpath.cpp b/test/testpath.cpp index 14b18cd3d..085ff411e 100644 --- a/test/testpath.cpp +++ b/test/testpath.cpp @@ -79,6 +79,7 @@ private: void getRelative() { std::vector basePaths; + basePaths.push_back(""); // Don't crash with empty paths basePaths.push_back("C:/foo"); basePaths.push_back("C:/bar/"); basePaths.push_back("C:/test.cpp");