Skip to content

Commit 9f054fb

Browse files
committed
Fixed crash when "-rp=" or "--relative-paths=" is given - print Error.
1 parent 9857f8d commit 9f054fb

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,18 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
254254
_settings->_relativePaths = true;
255255
else if (strncmp(argv[i], "-rp=", 4) == 0 || strncmp(argv[i], "--relative-paths=", 17) == 0) {
256256
_settings->_relativePaths = true;
257-
std::string paths = argv[i]+(argv[i][3]=='='?4:17);
258-
std::string::size_type pos;
259-
do {
260-
pos = paths.find(';');
261-
_settings->_basePaths.push_back(Path::fromNativeSeparators(paths.substr(0, pos)));
262-
paths.erase(0, pos+1);
263-
} while (pos != std::string::npos);
257+
if (argv[i][argv[i][3]=='='?4:17] != 0) {
258+
std::string paths = argv[i]+(argv[i][3]=='='?4:17);
259+
std::string::size_type pos;
260+
do {
261+
pos = paths.find(';');
262+
_settings->_basePaths.push_back(Path::fromNativeSeparators(paths.substr(0, pos)));
263+
paths.erase(0, pos+1);
264+
} while (pos != std::string::npos);
265+
} else {
266+
PrintMessage("cppcheck: No paths specified for the '" + std::string(argv[i]) + "' option.");
267+
return false;
268+
}
264269
}
265270

266271
// Write results in results.xml

lib/path.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ std::string Path::getFilenameExtensionInLowerCase(const std::string &path)
157157
std::string Path::getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths)
158158
{
159159
for (std::vector<std::string>::const_iterator i = basePaths.begin(); i != basePaths.end(); ++i) {
160-
if (absolutePath == *i) // Seems to be a file
160+
if (absolutePath == *i || i->empty()) // Seems to be a file, or path is empty
161161
continue;
162162

163163
bool endsWithSep = (*i)[i->length()-1] == '/';

test/testpath.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class TestPath : public TestFixture {
7979

8080
void getRelative() {
8181
std::vector<std::string> basePaths;
82+
basePaths.push_back(""); // Don't crash with empty paths
8283
basePaths.push_back("C:/foo");
8384
basePaths.push_back("C:/bar/");
8485
basePaths.push_back("C:/test.cpp");

0 commit comments

Comments
 (0)