This is an archive of the discontinued LLVM Phabricator instance.

[clang] Fix a crash during code completion
ClosedPublic

Authored by adamcz on Jun 1 2021, 10:51 AM.

Details

Summary

During code completion, lookupInDeclContext() calls
CodeCompletionDeclConsumer::FoundDecl(),which can mutate StoredDeclsMap,
over which lookupInDeclContext() iterates. This can lead to invalidation
of iterators and an assert()-crash.

Example code where this happens:
#include <list>
int main() {

std::list<int>;
std::^

}
with code completion on ^ with -std=c++20.

I do not have a repro case that does not need standard library.

This fix stores pointers to NamedDecls in a temporary vector, then
visits them outside of the main loop, when StoredDeclsMap iterators are
gone.

Diff Detail

Event Timeline

adamcz requested review of this revision.Jun 1 2021, 10:51 AM
adamcz created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptJun 1 2021, 10:51 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

This is https://github.com/clangd/clangd/issues/771

Sending for review mostly to see if you have any comments on this approach. Trying to reproduce this without <list>, <vector> or something like this didn't work for me so far, I must be holding it wrong or something ;-) Still, submitting without a nice test case seems wrong.
The whole C++20, as far as I can tell, simply adds a bunch of code that's behind ifdef, which is what causes the crash. I don't think this is related to any C++20 features.

kadircet accepted this revision.Jun 7 2021, 1:11 AM

we discussed offline but i forgot to stamp this one. it would be nice to have a test case, but fix is relatively safe and getting a repro turned out to be hard (since it depends on a densemap growing).

thanks!

This revision is now accepted and ready to land.Jun 7 2021, 1:11 AM
This revision was landed with ongoing or failed builds.Jun 7 2021, 4:37 AM
This revision was automatically updated to reflect the committed changes.