[clangd] Fix a crash when reading an empty index file.

Summary:
Unfortunately, yaml::Input::setCurrentDocument() and yaml::Input::nextDocument() are
internal APIs, the way we use them may cause a nullptr accessing when
processing an empty YAML file.

Reviewers: ilya-biryukov

Subscribers: ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits

Differential Revision: https://reviews.llvm.org/D56442

llvm-svn: 350633
This commit is contained in:
Haojian Wu 2019-01-08 15:24:47 +00:00
parent 59e373fd63
commit 073d184ee3
2 changed files with 10 additions and 3 deletions

View File

@ -314,17 +314,20 @@ llvm::Expected<IndexFileIn> readYAML(llvm::StringRef Data) {
Arena; // store the underlying data of Position::FileURI.
llvm::UniqueStringSaver Strings(Arena);
llvm::yaml::Input Yin(Data, &Strings);
do {
while (Yin.setCurrentDocument()) {
llvm::yaml::EmptyContext Ctx;
VariantEntry Variant;
Yin >> Variant;
yamlize(Yin, Variant, true, Ctx);
if (Yin.error())
return llvm::errorCodeToError(Yin.error());
if (Variant.Symbol)
Symbols.insert(*Variant.Symbol);
if (Variant.Refs)
for (const auto &Ref : Variant.Refs->second)
Refs.insert(Variant.Refs->first, Ref);
} while (Yin.nextDocument());
Yin.nextDocument();
}
IndexFileIn Result;
Result.Symbols.emplace(std::move(Symbols).build());

View File

@ -91,6 +91,10 @@ MATCHER_P2(IncludeHeaderWithRef, IncludeHeader, References, "") {
return (arg.IncludeHeader == IncludeHeader) && (arg.References == References);
}
TEST(SerializationTest, NoCrashOnEmptyYAML) {
EXPECT_TRUE(bool(readIndexFile("")));
}
TEST(SerializationTest, YAMLConversions) {
auto In = readIndexFile(YAML);
EXPECT_TRUE(bool(In)) << In.takeError();