public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Fix unchecked use of CLASSTYPE_AS_BASE [PR113031]
@ 2023-12-16  0:20 Nathaniel Shead
  2023-12-16  3:41 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Nathaniel Shead @ 2023-12-16  0:20 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jason Merrill, Jonathan Wakely

Bootstrapped and regtested on x86_64-pc-linux-gnu with GLIBCXX_TESTSUITE_STDS=20
and RUNTESTFLAGS="--target_board=unix/-D_GLIBCXX_USE_CXX11_ABI=0".

-- >8 --

My previous patch (naively) assumed that a TREE_CODE of RECORD_TYPE or
UNION_TYPE was sufficient for optype to be considered a "class type".
However, this does not account for e.g. template type parameters of
record or union type. This patch corrects to check for CLASS_TYPE_P
before checking for as-base conversion.

	PR c++/113031

gcc/cp/ChangeLog:

	* constexpr.cc (cxx_fold_indirect_ref_1): Check for CLASS_TYPE
	before using CLASSTYPE_AS_BASE.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/pr113031.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
---
 gcc/cp/constexpr.cc                   |  3 ++-
 gcc/testsuite/g++.dg/cpp0x/pr113031.C | 34 +++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/pr113031.C

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index e1b2d27fc36..051f73fb73f 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -5709,7 +5709,8 @@ cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
 	  }
 
       /* Handle conversion to "as base" type.  */
-      if (CLASSTYPE_AS_BASE (optype) == type)
+      if (CLASS_TYPE_P (optype)
+	  && CLASSTYPE_AS_BASE (optype) == type)
 	return op;
 
       /* Handle conversion to an empty base class, which is represented with a
diff --git a/gcc/testsuite/g++.dg/cpp0x/pr113031.C b/gcc/testsuite/g++.dg/cpp0x/pr113031.C
new file mode 100644
index 00000000000..aecdc3fc4b2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/pr113031.C
@@ -0,0 +1,34 @@
+// PR c++/113031
+// { dg-do compile }
+
+template <typename> struct variant;
+
+template <typename _Types, typename _Tp>
+variant<_Types> __variant_cast(_Tp __rhs) { return static_cast<variant<_Types>&>(__rhs); }
+
+template <typename _Types>
+struct _Move_assign_base : _Types {
+  void operator=(_Move_assign_base __rhs) { __variant_cast<_Types>(__rhs); }
+};
+
+template <typename _Types>
+struct variant : _Move_assign_base<_Types> {
+  void emplace() {
+    variant __tmp;
+    *this = __tmp;
+  }
+};
+
+struct _Undefined_class {
+  struct _Nocopy_types {
+    void (_Undefined_class::*_M_member_pointer)();
+  };
+  struct function : _Nocopy_types {
+    struct optional {
+      void test03() {
+        variant<function> v;
+        v.emplace();
+      }
+    };
+  };
+};
-- 
2.42.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: Fix unchecked use of CLASSTYPE_AS_BASE [PR113031]
  2023-12-16  0:20 [PATCH] c++: Fix unchecked use of CLASSTYPE_AS_BASE [PR113031] Nathaniel Shead
@ 2023-12-16  3:41 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2023-12-16  3:41 UTC (permalink / raw)
  To: Nathaniel Shead, gcc-patches; +Cc: Jonathan Wakely

On 12/15/23 19:20, Nathaniel Shead wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu with GLIBCXX_TESTSUITE_STDS=20
> and RUNTESTFLAGS="--target_board=unix/-D_GLIBCXX_USE_CXX11_ABI=0".

OK, thanks.

> -- >8 --
> 
> My previous patch (naively) assumed that a TREE_CODE of RECORD_TYPE or
> UNION_TYPE was sufficient for optype to be considered a "class type".
> However, this does not account for e.g. template type parameters of
> record or union type. This patch corrects to check for CLASS_TYPE_P
> before checking for as-base conversion.
> 
> 	PR c++/113031
> 
> gcc/cp/ChangeLog:
> 
> 	* constexpr.cc (cxx_fold_indirect_ref_1): Check for CLASS_TYPE
> 	before using CLASSTYPE_AS_BASE.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/pr113031.C: New test.
> 
> Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> ---
>   gcc/cp/constexpr.cc                   |  3 ++-
>   gcc/testsuite/g++.dg/cpp0x/pr113031.C | 34 +++++++++++++++++++++++++++
>   2 files changed, 36 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/pr113031.C
> 
> diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
> index e1b2d27fc36..051f73fb73f 100644
> --- a/gcc/cp/constexpr.cc
> +++ b/gcc/cp/constexpr.cc
> @@ -5709,7 +5709,8 @@ cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
>   	  }
>   
>         /* Handle conversion to "as base" type.  */
> -      if (CLASSTYPE_AS_BASE (optype) == type)
> +      if (CLASS_TYPE_P (optype)
> +	  && CLASSTYPE_AS_BASE (optype) == type)
>   	return op;
>   
>         /* Handle conversion to an empty base class, which is represented with a
> diff --git a/gcc/testsuite/g++.dg/cpp0x/pr113031.C b/gcc/testsuite/g++.dg/cpp0x/pr113031.C
> new file mode 100644
> index 00000000000..aecdc3fc4b2
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/pr113031.C
> @@ -0,0 +1,34 @@
> +// PR c++/113031
> +// { dg-do compile }
> +
> +template <typename> struct variant;
> +
> +template <typename _Types, typename _Tp>
> +variant<_Types> __variant_cast(_Tp __rhs) { return static_cast<variant<_Types>&>(__rhs); }
> +
> +template <typename _Types>
> +struct _Move_assign_base : _Types {
> +  void operator=(_Move_assign_base __rhs) { __variant_cast<_Types>(__rhs); }
> +};
> +
> +template <typename _Types>
> +struct variant : _Move_assign_base<_Types> {
> +  void emplace() {
> +    variant __tmp;
> +    *this = __tmp;
> +  }
> +};
> +
> +struct _Undefined_class {
> +  struct _Nocopy_types {
> +    void (_Undefined_class::*_M_member_pointer)();
> +  };
> +  struct function : _Nocopy_types {
> +    struct optional {
> +      void test03() {
> +        variant<function> v;
> +        v.emplace();
> +      }
> +    };
> +  };
> +};


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-12-16  3:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-16  0:20 [PATCH] c++: Fix unchecked use of CLASSTYPE_AS_BASE [PR113031] Nathaniel Shead
2023-12-16  3:41 ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).