From: Nathaniel Shead Date: Tue, 9 Sep 2025 23:59:23 +0000 (+1000) Subject: c++: Fix null deref in maybe_diagnose_standard_trait [PR121859] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7fc9265ee5b20c4191b8fcad47234148c72f7b03;p=thirdparty%2Fgcc.git 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 --- diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index 4b20b791e5d..309ebc81324 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 00000000000..de99dcd13c8 --- /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 +struct S { + template + static constexpr bool foo = sizeof(T) == sizeof(U); +}; + +template void bar(U x) requires S::foo {} + +int main() { + bar(double{}); // { dg-error "no matching function" } +}