Skip to content
Snippets Groups Projects
Commit 1a8d6861 authored by Manuel Klimek's avatar Manuel Klimek
Browse files

Fixes crasher bug in JSONCompilationDatabase for invalid input.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156814 91177308-0d34-0410-b5e6-96231b3b80d8
parent 91720917
No related branches found
No related tags found
No related merge requests found
...@@ -222,10 +222,9 @@ bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { ...@@ -222,10 +222,9 @@ bool JSONCompilationDatabase::parse(std::string &ErrorMessage) {
ErrorMessage = "Expected object."; ErrorMessage = "Expected object.";
return false; return false;
} }
llvm::yaml::ScalarNode *Directory; llvm::yaml::ScalarNode *Directory = NULL;
llvm::yaml::ScalarNode *Command; llvm::yaml::ScalarNode *Command = NULL;
llvm::SmallString<8> FileStorage; llvm::yaml::ScalarNode *File = NULL;
llvm::StringRef File;
for (llvm::yaml::MappingNode::iterator KVI = Object->begin(), for (llvm::yaml::MappingNode::iterator KVI = Object->begin(),
KVE = Object->end(); KVE = Object->end();
KVI != KVE; ++KVI) { KVI != KVE; ++KVI) {
...@@ -242,20 +241,37 @@ bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { ...@@ -242,20 +241,37 @@ bool JSONCompilationDatabase::parse(std::string &ErrorMessage) {
} }
llvm::yaml::ScalarNode *KeyString = llvm::yaml::ScalarNode *KeyString =
llvm::dyn_cast<llvm::yaml::ScalarNode>((*KVI).getKey()); llvm::dyn_cast<llvm::yaml::ScalarNode>((*KVI).getKey());
if (KeyString == NULL) {
ErrorMessage = "Expected strings as key.";
return false;
}
llvm::SmallString<8> KeyStorage; llvm::SmallString<8> KeyStorage;
if (KeyString->getValue(KeyStorage) == "directory") { if (KeyString->getValue(KeyStorage) == "directory") {
Directory = ValueString; Directory = ValueString;
} else if (KeyString->getValue(KeyStorage) == "command") { } else if (KeyString->getValue(KeyStorage) == "command") {
Command = ValueString; Command = ValueString;
} else if (KeyString->getValue(KeyStorage) == "file") { } else if (KeyString->getValue(KeyStorage) == "file") {
File = ValueString->getValue(FileStorage); File = ValueString;
} else { } else {
ErrorMessage = ("Unknown key: \"" + ErrorMessage = ("Unknown key: \"" +
KeyString->getRawValue() + "\"").str(); KeyString->getRawValue() + "\"").str();
return false; return false;
} }
} }
IndexByFile[File].push_back( if (!File) {
ErrorMessage = "Missing key: \"file\".";
return false;
}
if (!Command) {
ErrorMessage = "Missing key: \"command\".";
return false;
}
if (!Directory) {
ErrorMessage = "Missing key: \"directory\".";
return false;
}
llvm::SmallString<8> FileStorage;
IndexByFile[File->getValue(FileStorage)].push_back(
CompileCommandRef(Directory, Command)); CompileCommandRef(Directory, Command));
} }
return true; return true;
......
...@@ -18,6 +18,26 @@ ...@@ -18,6 +18,26 @@
namespace clang { namespace clang {
namespace tooling { namespace tooling {
static void expectFailure(StringRef JSONDatabase, StringRef Explanation) {
std::string ErrorMessage;
EXPECT_EQ(NULL, JSONCompilationDatabase::loadFromBuffer(JSONDatabase,
ErrorMessage))
<< "Expected an error because of: " << Explanation;
}
TEST(JSONCompilationDatabase, ErrsOnInvalidFormat) {
expectFailure("", "Empty database");
expectFailure("{", "Invalid JSON");
expectFailure("[[]]", "Array instead of object");
expectFailure("[{\"a\":[]}]", "Array instead of value");
expectFailure("[{\"a\":\"b\"}]", "Unknown key");
expectFailure("[{[]:\"\"}]", "Incorrectly typed entry");
expectFailure("[{}]", "Empty entry");
expectFailure("[{\"directory\":\"\",\"command\":\"\"}]", "Missing file");
expectFailure("[{\"directory\":\"\",\"file\":\"\"}]", "Missing command");
expectFailure("[{\"command\":\"\",\"file\":\"\"}]", "Missing directory");
}
static CompileCommand findCompileArgsInJsonDatabase(StringRef FileName, static CompileCommand findCompileArgsInJsonDatabase(StringRef FileName,
StringRef JSONDatabase, StringRef JSONDatabase,
std::string &ErrorMessage) { std::string &ErrorMessage) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment