[cfe-commits] r156814 - in /cfe/trunk: lib/Tooling/CompilationDatabase.cpp unittests/Tooling/CompilationDatabaseTest.cpp

Manuel Klimek klimek at google.com
Tue May 15 04:46:07 PDT 2012


Author: klimek
Date: Tue May 15 06:46:07 2012
New Revision: 156814

URL: http://llvm.org/viewvc/llvm-project?rev=156814&view=rev
Log:
Fixes crasher bug in JSONCompilationDatabase for invalid input.


Modified:
    cfe/trunk/lib/Tooling/CompilationDatabase.cpp
    cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp

Modified: cfe/trunk/lib/Tooling/CompilationDatabase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/CompilationDatabase.cpp?rev=156814&r1=156813&r2=156814&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/CompilationDatabase.cpp (original)
+++ cfe/trunk/lib/Tooling/CompilationDatabase.cpp Tue May 15 06:46:07 2012
@@ -222,10 +222,9 @@
       ErrorMessage = "Expected object.";
       return false;
     }
-    llvm::yaml::ScalarNode *Directory;
-    llvm::yaml::ScalarNode *Command;
-    llvm::SmallString<8> FileStorage;
-    llvm::StringRef File;
+    llvm::yaml::ScalarNode *Directory = NULL;
+    llvm::yaml::ScalarNode *Command = NULL;
+    llvm::yaml::ScalarNode *File = NULL;
     for (llvm::yaml::MappingNode::iterator KVI = Object->begin(),
                                            KVE = Object->end();
          KVI != KVE; ++KVI) {
@@ -242,20 +241,37 @@
       }
       llvm::yaml::ScalarNode *KeyString =
         llvm::dyn_cast<llvm::yaml::ScalarNode>((*KVI).getKey());
+      if (KeyString == NULL) {
+        ErrorMessage = "Expected strings as key.";
+        return false;
+      }
       llvm::SmallString<8> KeyStorage;
       if (KeyString->getValue(KeyStorage) == "directory") {
         Directory = ValueString;
       } else if (KeyString->getValue(KeyStorage) == "command") {
         Command = ValueString;
       } else if (KeyString->getValue(KeyStorage) == "file") {
-        File = ValueString->getValue(FileStorage);
+        File = ValueString;
       } else {
         ErrorMessage = ("Unknown key: \"" +
                         KeyString->getRawValue() + "\"").str();
         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));
   }
   return true;

Modified: cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp?rev=156814&r1=156813&r2=156814&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp Tue May 15 06:46:07 2012
@@ -18,6 +18,26 @@
 namespace clang {
 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,
                                                     StringRef JSONDatabase,
                                                     std::string &ErrorMessage) {





More information about the cfe-commits mailing list