Skip to content

Commit 557e32e

Browse files
committed
[clang] SIGSEGV fix at clang::ASTContext::getRawCommentForDeclNoCacheImpl
The `File` might point to an invalid `FileID` when the AST is broken. That leads to clang/clangd crashes while processing comments. The relevant part of the crash is below ``` #4 0x00007f1d7fbf95bc std::_Rb_tree<unsigned int, std::pair<unsigned int const, clang::RawComment*>, std::_Select1st<std::pair<unsigned int const, clang::RawComment*>>, std::less<unsigned int>, std::allocator<std::pair<unsigned int const , clang::RawComment*>>>::_M_lower_bound(std::_Rb_tree_node<std::pair<unsigned int const, clang::RawComment*>> const*, std::_Rb_tree_node_base const*, unsigned int const&) const /usr/include/c++/8/bits/stl_tree.h:1911:2 #5 0x00007f1d7fbf95bc std::_Rb_tree<unsigned int, std::pair<unsigned int const, clang::RawComment*>, std::_Select1st<std::pair<unsigned int const, clang::RawComment*>>, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, clang::RawComment*>>>::lower_bound(unsigned int const&) const /usr/include/c++/8/bits/stl_tree.h:1214:56 #6 0x00007f1d7fbf95bc std::map<unsigned int, clang::RawComment*, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, clang::RawComment*>>>::lower_bound(unsigned int const&) const /usr/include/c++/8/bits/stl_map.h:1264:36 #7 0x00007f1d7fbf95bc clang::ASTContext::getRawCommentForDeclNoCacheImpl(clang::Decl const*, clang::SourceLocation, std::map<unsigned int, clang::RawComment*, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, clang::RawComment*>>> const&) const /home/ivanmurashko/local/llvm-project/clang/lib/AST/ASTContext.cpp:226:57 ``` The corresponding LIT test that reproduces the crash was also added Same issue is described at https://bugs.llvm.org/show_bug.cgi?id=49707 Reviewed By: gribozavr2 Differential Revision: https://reviews.llvm.org/D131675
1 parent 3486b1b commit 557e32e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

clang/lib/AST/ASTContext.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
298298
return nullptr;
299299

300300
const FileID File = SourceMgr.getDecomposedLoc(DeclLoc).first;
301+
if (!File.isValid()) {
302+
return nullptr;
303+
}
301304
const auto CommentsInThisFile = Comments.getCommentsInFile(File);
302305
if (!CommentsInThisFile || CommentsInThisFile->empty())
303306
return nullptr;

clang/test/AST/ast-crash-doc.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
4+
// RUN: %clang_cc1 -emit-module -x c++ -fmodules -I %t/Inputs -fmodule-name=aa %t/Inputs/module.modulemap -o %t/aa.pcm
5+
// RUN: rm %t/Inputs/b.h
6+
// RUN: not %clang_cc1 -x c++ -Wdocumentation -ast-dump-all -fmodules -I %t/Inputs -fmodule-file=%t/aa.pcm %t/test.cpp | FileCheck %s
7+
8+
//--- Inputs/module.modulemap
9+
module aa {
10+
header "a.h"
11+
header "b.h"
12+
}
13+
14+
//--- Inputs/a.h
15+
// empty file
16+
17+
//--- Inputs/b.h
18+
/// test foo @return
19+
int foo();
20+
21+
22+
//--- test.cpp
23+
#include "a.h"
24+
25+
/// test comment at the primary file
26+
27+
int a = foo();
28+
29+
30+
// CHECK: TranslationUnitDecl

0 commit comments

Comments
 (0)