[clangd][Dex] Fix crashes when building trigrams for empty identifier

This commit is contained in:
Kadir Cetinkaya 2021-12-13 15:57:51 +01:00
parent 6c85a49e22
commit a47af1ac34
No known key found for this signature in database
GPG Key ID: E39E36B8D2057ED6
2 changed files with 5 additions and 0 deletions

View File

@ -28,6 +28,7 @@ namespace dex {
// Produce trigrams (including duplicates) and pass them to Out().
template <typename Func>
static void identifierTrigrams(llvm::StringRef Identifier, Func Out) {
assert(!Identifier.empty());
// Apply fuzzy matching text segmentation.
llvm::SmallVector<CharRole> Roles(Identifier.size());
calculateRoles(Identifier,
@ -104,6 +105,9 @@ void generateIdentifierTrigrams(llvm::StringRef Identifier,
// The magic number was tuned by running IndexBenchmark.DexBuild.
constexpr unsigned ManyTrigramsIdentifierThreshold = 14;
Result.clear();
if (Identifier.empty())
return;
if (Identifier.size() < ManyTrigramsIdentifierThreshold) {
identifierTrigrams(Identifier, [&](Trigram T) {
if (!llvm::is_contained(Result, T))

View File

@ -415,6 +415,7 @@ TEST(DexTrigrams, IdentifierTrigrams) {
"cdg", "def", "deg", "dgh", "dgk", "efg", "egh",
"egk", "fgh", "fgk", "ghi", "ghk", "gkl", "hij",
"hik", "hkl", "ijk", "ikl", "jkl", "klm"}));
EXPECT_THAT(identifierTrigramTokens(""), IsEmpty());
}
TEST(DexTrigrams, QueryTrigrams) {