public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Alexandre Oliva <oliva@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: David Malcolm <dmalcolm@redhat.com>,
	Rainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>,
	Mike Stump <mikestump@comcast.net>,
	Jason Merrill <jason@redhat.com>, Nathan Sidwell <nathan@acm.org>
Subject: Re: [PATCH #3/4] warn on cast of pointer to packed plus constant
Date: Sun, 19 Nov 2023 23:34:17 -0300	[thread overview]
Message-ID: <orjzqdqhjq.fsf@lxoliva.fsfla.org> (raw)
In-Reply-To: <orh6liry88.fsf@lxoliva.fsfla.org> (Alexandre Oliva's message of "Sun, 19 Nov 2023 04:36:23 -0300")


c-c++-common/analyzer/null-deref-pr108251-smp-fetch_ssl_fc_has_early.c
gets an unaligned pointer value warning on -fshort-enums targets in C,
but not in C++.  The former simplifies the offset-and-cast expression
enough that check_and_warn_address_or_pointer_of_packed_member finds
no more than a type cast of the base pointer, but in C++, the entire
expression, with cast, constant offsetting, and cast again, is
retained, and that's too much for the warning code.

Or rather it was.  It's easy enough to take the base pointer from
PLUS_POINTER_EXPR, and a constant offset can't possibly increase
alignment for just any pointer of laxer alignment, so we can safely
disregard the offset.

This should improve the warning even in C, if the packed enum is at a
nonzero offset into the containing struct.

Regstrapped on x86_64-linux-gnu, also tested on arm-eabi with default
cpu on trunk, and with tms570 on gcc-13.  Ok to install?


for  gcc/c-family/ChangeLog

	* c-warn.cc
	(check_and_warn_address_or_pointer_of_packed_member): Take the
	base pointer from PLUS_POINTER_EXPR when the addend is
	constant.
---
 gcc/c-family/c-warn.cc |   16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc
index d2938b91043d3..2ef73d7088f22 100644
--- a/gcc/c-family/c-warn.cc
+++ b/gcc/c-family/c-warn.cc
@@ -3108,10 +3108,20 @@ check_and_warn_address_or_pointer_of_packed_member (tree type, tree rhs)
 
   do
     {
-      while (TREE_CODE (rhs) == COMPOUND_EXPR)
-	rhs = TREE_OPERAND (rhs, 1);
-      orig_rhs = rhs;
+      do
+	{
+	  orig_rhs = rhs;
+	  while (TREE_CODE (rhs) == COMPOUND_EXPR)
+	    rhs = TREE_OPERAND (rhs, 1);
+	  /* Constants can't increase the alignment.  */
+	  while (TREE_CODE (rhs) == POINTER_PLUS_EXPR
+		 && TREE_CONSTANT (TREE_OPERAND (rhs, 1)))
+	    rhs = TREE_OPERAND (rhs, 0);
+	}
+      while (orig_rhs != rhs);
       STRIP_NOPS (rhs);
+      while (TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
+	rhs = TREE_OPERAND (rhs, 0);
       nop_p |= orig_rhs != rhs;
     }
   while (orig_rhs != rhs);

-- 
Alexandre Oliva, happy hacker            https://FSFLA.org/blogs/lxo/
   Free Software Activist                   GNU Toolchain Engineer
More tolerance and less prejudice are key for inclusion and diversity
Excluding neuro-others for not behaving ""normal"" is *not* inclusive

  parent reply	other threads:[~2023-11-20  2:34 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-19  7:36 [PATCH] testsuite: analyzer: expect alignment warning with -fshort-enums Alexandre Oliva
2023-11-19 15:13 ` Jeff Law
2023-11-20 22:19   ` David Malcolm
2023-11-20  2:33 ` [PATCH #2/4] c++: mark short-enums as packed Alexandre Oliva
2023-11-20 19:55   ` Jason Merrill
2023-11-22  8:17     ` Alexandre Oliva
2023-11-22 18:12       ` Jason Merrill
2023-11-22 18:26         ` Jason Merrill
2023-11-29  9:39           ` Alexandre Oliva
2023-11-29 19:03             ` Jason Merrill
2023-11-30  7:21               ` Alexandre Oliva
2023-11-20  2:34 ` Alexandre Oliva [this message]
2023-11-20  2:34 ` [PATCH #4/4] testsuite: discard c++ exclusion on underaligned pointer warning Alexandre Oliva
2023-11-23 20:27   ` Mike Stump

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=orjzqdqhjq.fsf@lxoliva.fsfla.org \
    --to=oliva@adacore.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=mikestump@comcast.net \
    --cc=nathan@acm.org \
    --cc=ro@CeBiTec.Uni-Bielefeld.DE \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).