public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Bernd Schmidt <bernds_cb1@t-online.de>
To: Martin Sebor <msebor@gmail.com>,
	Bernd Schmidt <bschmidt@redhat.com>,
	Gcc Patch List <gcc-patches@gcc.gnu.org>,
	Joseph Myers <joseph@codesourcery.com>
Subject: Re: [PATCH] c/67882 - improve -Warray-bounds for invalid offsetof
Date: Tue, 20 Oct 2015 13:21:00 -0000	[thread overview]
Message-ID: <56263F80.1090203@t-online.de> (raw)
In-Reply-To: <56215158.5040404@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1342 bytes --]

On 10/16/2015 09:34 PM, Martin Sebor wrote:
> Thank you for the review. Attached is an updated patch that hopefully
> addresses all your comments. I ran the check_GNU_style.sh script on
> it to make sure I didn't miss something.  I've also added replies to
> a few of your comments below.

Ok, thanks. However - the logic around the context struct in the patch 
still seems fairly impenetrable to me, and I've been wondering whether a 
simpler approach wouldn't work just as well. I came up with the patch 
below, which just passes a tree_code context to recursive invocations 
and increasing the upper bound by one only if not in an ARRAY_REF or 
COMPONENT_REF context.

As far as I can tell, this produces the same warnings on the testcase 
(modulo differences in type printout), except for the final few FA5_7 
testcases, which I had doubts about anyway:

typedef struct FA5_7 {
   int i;
   char a5_7 [5][7];
} FA5_7;

     __builtin_offsetof (FA5_7, a5_7 [0][7]),         // { dg-warning 
"index" }
     __builtin_offsetof (FA5_7, a5_7 [1][7]),         // { dg-warning 
"index" }
     __builtin_offsetof (FA5_7, a5_7 [5][0]),         // { dg-warning 
"index" }
     __builtin_offsetof (FA5_7, a5_7 [5][7]),         // { dg-warning 
"index" }

Why wouldn't at least the first two be convered by the 
one-past-the-end-is-ok rule?


Bernd


[-- Attachment #2: offsetof.diff --]
[-- Type: text/x-patch, Size: 2096 bytes --]

Index: gcc/c-family/c-common.c
===================================================================
--- gcc/c-family/c-common.c	(revision 229049)
+++ gcc/c-family/c-common.c	(working copy)
@@ -10589,11 +10589,11 @@ c_common_to_target_charset (HOST_WIDE_IN
    traditional rendering of offsetof as a macro.  Return the folded result.  */
 
 tree
-fold_offsetof_1 (tree expr)
+fold_offsetof_1 (tree expr, enum tree_code ctx)
 {
   tree base, off, t;
-
-  switch (TREE_CODE (expr))
+  tree_code code = TREE_CODE (expr);
+  switch (code)
     {
     case ERROR_MARK:
       return expr;
@@ -10617,7 +10617,7 @@ fold_offsetof_1 (tree expr)
       return TREE_OPERAND (expr, 0);
 
     case COMPONENT_REF:
-      base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
+      base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
       if (base == error_mark_node)
 	return base;
 
@@ -10634,7 +10634,7 @@ fold_offsetof_1 (tree expr)
       break;
 
     case ARRAY_REF:
-      base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
+      base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
       if (base == error_mark_node)
 	return base;
 
@@ -10649,8 +10649,9 @@ fold_offsetof_1 (tree expr)
 	      && !tree_int_cst_equal (upbound,
 				      TYPE_MAX_VALUE (TREE_TYPE (upbound))))
 	    {
-	      upbound = size_binop (PLUS_EXPR, upbound,
-				    build_int_cst (TREE_TYPE (upbound), 1));
+	      if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
+		upbound = size_binop (PLUS_EXPR, upbound,
+				      build_int_cst (TREE_TYPE (upbound), 1));
 	      if (tree_int_cst_lt (upbound, t))
 		{
 		  tree v;
Index: gcc/c-family/c-common.h
===================================================================
--- gcc/c-family/c-common.h	(revision 229049)
+++ gcc/c-family/c-common.h	(working copy)
@@ -1029,7 +1029,7 @@ extern bool c_dump_tree (void *, tree);
 
 extern void verify_sequence_points (tree);
 
-extern tree fold_offsetof_1 (tree);
+extern tree fold_offsetof_1 (tree, tree_code ctx = ERROR_MARK);
 extern tree fold_offsetof (tree);
 
 /* Places where an lvalue, or modifiable lvalue, may be required.

  reply	other threads:[~2015-10-20 13:20 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-09  2:55 Martin Sebor
2015-10-15 21:59 ` [PING] " Martin Sebor
2015-10-16 12:28 ` Bernd Schmidt
2015-10-16 17:27   ` Joseph Myers
2015-10-16 19:34   ` Martin Sebor
2015-10-20 13:21     ` Bernd Schmidt [this message]
2015-10-20 15:33       ` Martin Sebor
2015-10-20 15:52         ` Bernd Schmidt
2015-10-20 16:57           ` Martin Sebor
2015-10-20 17:11             ` Joseph Myers
2015-10-20 19:10               ` Martin Sebor
2015-10-20 16:54         ` Joseph Myers
2015-10-20 20:36           ` Bernd Schmidt
2015-10-20 22:19             ` Joseph Myers
2015-10-23 11:17               ` Bernd Schmidt
2015-10-23 15:15                 ` Martin Sebor
2015-10-23 16:53                   ` Joseph Myers
2015-10-23 17:45                     ` Bernd Schmidt
2015-10-23 20:54                       ` Martin Sebor
2015-10-26 11:44                         ` Bernd Schmidt
2015-10-26 11:51                           ` Jakub Jelinek
2015-10-26 12:01                             ` Bernd Schmidt
2015-10-26 12:04                               ` Jakub Jelinek
2015-10-26 12:32                                 ` Richard Biener
2015-10-27 11:18                                   ` Bernd Schmidt
2015-11-03 19:15                         ` Martin Sebor
2015-11-07 23:38               ` Segher Boessenkool
2015-11-09 22:46                 ` Martin Sebor
2015-11-10  0:02                 ` Joseph Myers
  -- strict thread matches above, loose matches on Subject: below --
2015-10-09  2:49 Martin Sebor

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=56263F80.1090203@t-online.de \
    --to=bernds_cb1@t-online.de \
    --cc=bschmidt@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    --cc=msebor@gmail.com \
    /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).