https://gcc.gnu.org/g:7fc9265ee5b20c4191b8fcad47234148c72f7b03

commit r16-3744-g7fc9265ee5b20c4191b8fcad47234148c72f7b03
Author: Nathaniel Shead <[email protected]>
Date:   Wed Sep 10 09:59:23 2025 +1000

    c++: Fix null deref in maybe_diagnose_standard_trait [PR121859]
    
    A static member template doesn't always have a DECL_INITIAL, as in the
    below testcase, and so checking its TREE_CODE performs a null-pointer
    dereference.  I don't think we need to specially detect this case as
    traits we care about are unlikely to be members, so this just adds the
    missing null check.
    
            PR c++/121859
    
    gcc/cp/ChangeLog:
    
            * constraint.cc (maybe_diagnose_standard_trait): Check if expr
            is non-null.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp2a/concepts-traits5.C: New test.
    
    Signed-off-by: Nathaniel Shead <[email protected]>

Diff:
---
 gcc/cp/constraint.cc                          |  2 +-
 gcc/testsuite/g++.dg/cpp2a/concepts-traits5.C | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 4b20b791e5d1..309ebc813243 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3360,7 +3360,7 @@ maybe_diagnose_standard_trait (location_t loc, tree expr)
        }
     }
 
-  if (TREE_CODE (expr) == TRAIT_EXPR)
+  if (expr && TREE_CODE (expr) == TRAIT_EXPR)
     {
       diagnose_trait_expr (loc, expr, args);
       return true;
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-traits5.C 
b/gcc/testsuite/g++.dg/cpp2a/concepts-traits5.C
new file mode 100644
index 000000000000..de99dcd13c89
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-traits5.C
@@ -0,0 +1,14 @@
+// PR c++/121859
+// { dg-do compile { target c++20 } }
+
+template <typename T>
+struct S {
+  template <typename U>
+  static constexpr bool foo = sizeof(T) == sizeof(U);
+};
+
+template <typename U> void bar(U x) requires S<char>::foo<U> {}
+
+int main() {
+  bar(double{});  // { dg-error "no matching function" }
+}

Reply via email to