[llvm-branch-commits] [clang] a45dd85 - [Concepts] Fix a deserialization crash.

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Aug 3 06:50:36 PDT 2020


Author: Haojian Wu
Date: 2020-08-03T15:46:42+02:00
New Revision: a45dd85fe4ccf721dc5ab01768c79bce73ff3474

URL: https://github.com/llvm/llvm-project/commit/a45dd85fe4ccf721dc5ab01768c79bce73ff3474
DIFF: https://github.com/llvm/llvm-project/commit/a45dd85fe4ccf721dc5ab01768c79bce73ff3474.diff

LOG: [Concepts] Fix a deserialization crash.

`TemplateTypeParmDecl::hasTypeConstraint` is not a safe guard for
checking `TemplateTypeParmDecl::getTypeConstraint()` result is null.

in somecases (e.g. implicit deduction guide templates synthesized from the
constructor, immediately-declared constraint is not formed because of an error),
hasTypeConstraint returns false, and getTypeConstraint returns a nullptr.

Fix https://bugs.llvm.org/show_bug.cgi?id=46790

Differential Revision: https://reviews.llvm.org/D84455

(cherry picked from commit 73c12bd8ff1a9cd8375a357ea06f171e127ec1b8)

Added: 
    clang/test/PCH/cxx2a-constraints-crash.cpp

Modified: 
    clang/lib/Serialization/ASTReaderDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp
index eef4ab16ec15..117eb598bd5e 100644
--- a/clang/lib/Serialization/ASTReaderDecl.cpp
+++ b/clang/lib/Serialization/ASTReaderDecl.cpp
@@ -2912,9 +2912,11 @@ static bool isSameTemplateParameter(const NamedDecl *X,
       return false;
     if (TX->hasTypeConstraint() != TY->hasTypeConstraint())
       return false;
-    if (TX->hasTypeConstraint()) {
-      const TypeConstraint *TXTC = TX->getTypeConstraint();
-      const TypeConstraint *TYTC = TY->getTypeConstraint();
+    const TypeConstraint *TXTC = TX->getTypeConstraint();
+    const TypeConstraint *TYTC = TY->getTypeConstraint();
+    if (!TXTC != !TYTC)
+      return false;
+    if (TXTC && TYTC) {
       if (TXTC->getNamedConcept() != TYTC->getNamedConcept())
         return false;
       if (TXTC->hasExplicitTemplateArgs() != TYTC->hasExplicitTemplateArgs())

diff  --git a/clang/test/PCH/cxx2a-constraints-crash.cpp b/clang/test/PCH/cxx2a-constraints-crash.cpp
new file mode 100644
index 000000000000..637c55f0c879
--- /dev/null
+++ b/clang/test/PCH/cxx2a-constraints-crash.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -std=c++2a -emit-pch %s -o %t
+// RUN: %clang_cc1 -std=c++2a -include-pch %t -verify %s
+
+// expected-no-diagnostics
+
+#ifndef HEADER
+#define HEADER
+
+template <typename T, typename U>
+concept not_same_as = true;
+
+template <int Kind>
+struct subrange {
+  template <not_same_as<int> R>
+  subrange(R) requires(Kind == 0);
+
+  template <not_same_as<int> R>
+  subrange(R) requires(Kind != 0);
+};
+
+template <typename R>
+subrange(R) -> subrange<42>;
+
+int main() {
+  int c;
+  subrange s(c);
+}
+
+#endif


        


More information about the llvm-branch-commits mailing list