Skip to content

Commit 0364baf

Browse files
authored
[clang] Fix possible nullptr deref in BuildCXXNestedNameSpecifier (llvm#166995)
There is a possible nullptr deref in BuildCXXNestedNameSpecifier when calling ExtendNestedNameSpecifier or using isa<>. This initially showed up as a crash in clangd, that didn't manifest in when compiling w/ clang. The reduced test case added in this patch, however does expose the issue in clang. Testing locally shows that both this test case and the original clangd issue are fixed by checking the validity of the pointer before trying to dispatch. Since all code paths require the pointer to be valid (usually by virtue of a dyn_cast or isa<> check), there should be no functional difference. Fixes llvm#166843
1 parent efc83cc commit 0364baf

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

clang/lib/Sema/SemaCXXScopeSpec.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,11 @@ bool Sema::BuildCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo,
780780

781781
if (!Found.empty()) {
782782
const auto *ND = Found.getAsSingle<NamedDecl>();
783+
if (!ND) {
784+
Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace)
785+
<< IdInfo.Identifier << getLangOpts().CPlusPlus;
786+
return true;
787+
}
783788
if (::ExtendNestedNameSpecifier(*this, SS, ND, IdInfo.IdentifierLoc,
784789
IdInfo.CCLoc)) {
785790
const Type *T = SS.getScopeRep().getAsType();

clang/test/Sema/PR166843.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang_cc1 -fsyntax-only %s -verify
2+
namespace a {
3+
template <class b>
4+
void c() {
5+
((::c::x)); // expected-error {{'c' is not a class, namespace, or enumeration}}
6+
}
7+
}

0 commit comments

Comments
 (0)