Skip to content

Commit 11b20d7

Browse files
authored
[clang] Fix an out-of-bound crash when checking template partial specializations. (llvm#86794)
I found this issue (a separate one) during the investigation of llvm#86757, the crash is similar in substituteParameterMappings, but at different inner places. This was an out-of-bound issue where we access front element in an empty written template argument list to get the instantiation source range. This patch fixes it by adding a proper guard.
1 parent b43ec8e commit 11b20d7

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ Bug Fixes to C++ Support
454454
- Fix a crash when instantiating a lambda that captures ``this`` outside of its context. Fixes (#GH85343).
455455
- Fix an issue where a namespace alias could be defined using a qualified name (all name components
456456
following the first `::` were ignored).
457+
- Fix an out-of-bounds crash when checking the validity of template partial specializations. (part of #GH86757).
457458

458459
Bug Fixes to AST Handling
459460
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaConcept.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,10 +1269,18 @@ substituteParameterMappings(Sema &S, NormalizedConstraint &N,
12691269
: SourceLocation()));
12701270
Atomic.ParameterMapping.emplace(TempArgs, OccurringIndices.count());
12711271
}
1272+
SourceLocation InstLocBegin =
1273+
ArgsAsWritten->arguments().empty()
1274+
? ArgsAsWritten->getLAngleLoc()
1275+
: ArgsAsWritten->arguments().front().getSourceRange().getBegin();
1276+
SourceLocation InstLocEnd =
1277+
ArgsAsWritten->arguments().empty()
1278+
? ArgsAsWritten->getRAngleLoc()
1279+
: ArgsAsWritten->arguments().front().getSourceRange().getEnd();
12721280
Sema::InstantiatingTemplate Inst(
1273-
S, ArgsAsWritten->arguments().front().getSourceRange().getBegin(),
1281+
S, InstLocBegin,
12741282
Sema::InstantiatingTemplate::ParameterMappingSubstitution{}, Concept,
1275-
ArgsAsWritten->arguments().front().getSourceRange());
1283+
{InstLocBegin, InstLocEnd});
12761284
if (S.SubstTemplateArguments(*Atomic.ParameterMapping, MLTAL, SubstArgs))
12771285
return true;
12781286

clang/test/SemaTemplate/concepts.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -std=c++20 -verify %s
1+
// RUN: %clang_cc1 -std=c++20 -ferror-limit 0 -verify %s
22

33
namespace PR47043 {
44
template<typename T> concept True = true;
@@ -1114,3 +1114,11 @@ void foo() {
11141114
}
11151115

11161116
} // namespace GH64808
1117+
1118+
namespace GH86757_1 {
1119+
template <typename...> concept b = false;
1120+
template <typename> concept c = b<>;
1121+
template <typename d> concept f = c< d >;
1122+
template <f> struct e; // expected-note {{}}
1123+
template <f d> struct e<d>; // expected-error {{class template partial specialization is not more specialized than the primary template}}
1124+
}

0 commit comments

Comments
 (0)