public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/ARM/heads/morello)] cp: Fix pointer comparison bug with capabilities
@ 2022-07-12 10:36 Alex Coplan
  0 siblings, 0 replies; only message in thread
From: Alex Coplan @ 2022-07-12 10:36 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:923d7b49cbd7a4e77334d5b7e3e184e85800063d

commit 923d7b49cbd7a4e77334d5b7e3e184e85800063d
Author: Alex Coplan <alex.coplan@arm.com>
Date:   Tue Jun 21 14:20:33 2022 +0100

    cp: Fix pointer comparison bug with capabilities
    
    This fixes a wrong code bug when comparing capability pointers. In
    cp_build_binary_op we would of course do pointer comparison in the
    non-capability type to get the correct CHERI C/C++ semantics, so we
    would just directly convert both operands to the non-capability type.
    This means we miss the conversion via the common pointer type, which is
    problematic when this conversion is not a no-op (e.g. converting from a
    pointer to a derived class to a pointer to a base class with multiple
    inheritance where a "this adjustment" is necessary).
    
    This patch fixes the bug by first converting to the common pointer type
    before converting to the final non-capability result type.

Diff:
---
 gcc/cp/typeck.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index b2a4566a67d..753191eff8d 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -5790,6 +5790,21 @@ cp_build_binary_op (const op_location_t &location,
   if (! converted)
     {
       warning_sentinel w (warn_sign_conversion, short_compare);
+
+      /* If we have a common capability type which is distinct from the final
+	 result type, first convert both operands to the common capability type.
+	 This is important when the conversion is not a no-op, e.g. for pointers
+	 to different classes within the same class hierarchy, we may have to
+	 perform a "this adjustment".  */
+      if (cap_result_type
+	  && result_type != cap_result_type)
+	{
+	  if (!same_type_p (TREE_TYPE (op0), cap_result_type))
+	    op0 = cp_convert_and_check (cap_result_type, op0, complain);
+	  if (!same_type_p (TREE_TYPE (op1), cap_result_type))
+	    op1 = cp_convert_and_check (cap_result_type, op1, complain);
+	}
+
       if (!same_type_p (TREE_TYPE (op0), result_type))
 	{
 	  op0 = cp_convert_and_check (result_type, op0, complain);


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-12 10:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-12 10:36 [gcc(refs/vendors/ARM/heads/morello)] cp: Fix pointer comparison bug with capabilities Alex Coplan

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).