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
11 changes: 11 additions & 0 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,17 @@ bool ImportProject::importCompileCommands(std::istream &istr)

for (const picojson::value &fileInfo : compileCommands.get<picojson::array>()) {
picojson::object obj = fileInfo.get<picojson::object>();

if (obj.count("directory") == 0) {
printError("'directory' field in compilation database entry missing");
return false;
}

if (!obj["directory"].is<std::string>()) {
printError("'directory' field in compilation database entry is not a string");
return false;
}

std::string dirpath = Path::fromNativeSeparators(obj["directory"].get<std::string>());

/* CMAKE produces the directory without trailing / so add it if not
Expand Down
23 changes: 23 additions & 0 deletions test/testimportproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class TestImportProject : public TestFixture {
TEST_CASE(importCompileCommands13); // #13333: duplicate file entries
TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section
TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json
TEST_CASE(importCompileCommandsDirectoryMissing); // 'directory' field missing
TEST_CASE(importCompileCommandsDirectoryInvalid); // 'directory' field not a string
TEST_CASE(importCppcheckGuiProject);
TEST_CASE(ignorePaths);
}
Expand Down Expand Up @@ -384,6 +386,27 @@ class TestImportProject : public TestFixture {
ASSERT_EQUALS("cppcheck: error: no 'arguments' or 'command' field found in compilation database entry\n", GET_REDIRECT_OUTPUT);
}

void importCompileCommandsDirectoryMissing() const {
REDIRECT;
constexpr char json[] = "[ { \"file\": \"src.mm\" } ]";
std::istringstream istr(json);
TestImporter importer;
ASSERT_EQUALS(false, importer.importCompileCommands(istr));
ASSERT_EQUALS(0, importer.fileSettings.size());
ASSERT_EQUALS("cppcheck: error: 'directory' field in compilation database entry missing\n", GET_REDIRECT_OUTPUT);
}

void importCompileCommandsDirectoryInvalid() const {
REDIRECT;
constexpr char json[] = "[ { \"directory\": 123,"
"\"file\": \"src.mm\" } ]";
std::istringstream istr(json);
TestImporter importer;
ASSERT_EQUALS(false, importer.importCompileCommands(istr));
ASSERT_EQUALS(0, importer.fileSettings.size());
ASSERT_EQUALS("cppcheck: error: 'directory' field in compilation database entry is not a string\n", GET_REDIRECT_OUTPUT);
}

void importCppcheckGuiProject() const {
REDIRECT;
constexpr char xml[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Expand Down
Loading