From 1a8d6861278051b2109c98baf6a7478a6f3f98aa Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Tue, 15 May 2012 11:46:07 +0000 Subject: 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 --- unittests/Tooling/CompilationDatabaseTest.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'unittests/Tooling/CompilationDatabaseTest.cpp') diff --git a/unittests/Tooling/CompilationDatabaseTest.cpp b/unittests/Tooling/CompilationDatabaseTest.cpp index 68d2896f12..7753c15410 100644 --- a/unittests/Tooling/CompilationDatabaseTest.cpp +++ b/unittests/Tooling/CompilationDatabaseTest.cpp @@ -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) { -- cgit v1.2.3-18-g5258