aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Basic/SourceManager.cpp
diff options
context:
space:
mode:
authorIvan Murashko <ivanmurashko@fb.com>2022-08-10 16:36:19 +0100
committerIvan Murashko <ivan.murashko@gmail.com>2022-08-10 16:37:42 +0100
commite78c80e3d622d455934102dabd765a60ac917739 (patch)
tree7ac7514a37321481e048a910d02efba71c2f19f6 /clang/lib/Basic/SourceManager.cpp
parentb1009bbd9e3d5acd85e92cb7f9a1109d2e72967c (diff)
downloadllvm-e78c80e3d622d455934102dabd765a60ac917739.zip
llvm-e78c80e3d622d455934102dabd765a60ac917739.tar.gz
llvm-e78c80e3d622d455934102dabd765a60ac917739.tar.bz2
[clang] SourceManager: fix at SourceManager::getFileIDLoaded for the case of invalid SLockEntry
There is a fix for the search procedure at `SourceManager::getFileIDLoaded`. It might return an invalid (not loaded) entry. That might cause clang/clangd crashes. At my case the scenario was the following: - `SourceManager::getFileIDLoaded` returned an invalid file id for a non loaded entry and incorrectly set `SourceManager::LastFileIDLookup` to the value - `getSLocEntry` returned `SourceManager::FakeSLocEntryForRecovery` introduced at [D89748](https://reviews.llvm.org/D89748). - The result entry is not tested at `SourceManager::isOffsetInFileID`and as result the call `return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset();` returned `true` value because `FID.ID+1` pointed to a non-fake entry - The tested offset was marked as one that belonged to the fake `SLockEntry` Such behaviour might cause a weird clangd crash when preamble contains some header files that were removed just after the preamble created. Unfortunately it's not so easy to reproduce the crash in the form of a LIT test thus I provided the fix only. Test Plan: ``` ninja check-clang ``` Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D130847
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r--clang/lib/Basic/SourceManager.cpp9
1 files changed, 6 insertions, 3 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 98e731e..38545d1 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -877,9 +877,12 @@ FileID SourceManager::getFileIDLoaded(SourceLocation::UIntTy SLocOffset) const {
I = (-LastID - 2) + 1;
unsigned NumProbes;
+ bool Invalid = false;
for (NumProbes = 0; NumProbes < 8; ++NumProbes, ++I) {
// Make sure the entry is loaded!
- const SrcMgr::SLocEntry &E = getLoadedSLocEntry(I);
+ const SrcMgr::SLocEntry &E = getLoadedSLocEntry(I, &Invalid);
+ if (Invalid)
+ return FileID(); // invalid entry.
if (E.getOffset() <= SLocOffset) {
FileID Res = FileID::get(-int(I) - 2);
LastFileIDLookup = Res;
@@ -897,8 +900,8 @@ FileID SourceManager::getFileIDLoaded(SourceLocation::UIntTy SLocOffset) const {
while (true) {
++NumProbes;
unsigned MiddleIndex = (LessIndex - GreaterIndex) / 2 + GreaterIndex;
- const SrcMgr::SLocEntry &E = getLoadedSLocEntry(MiddleIndex);
- if (E.getOffset() == 0)
+ const SrcMgr::SLocEntry &E = getLoadedSLocEntry(MiddleIndex, &Invalid);
+ if (Invalid)
return FileID(); // invalid entry.
++NumProbes;