public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: dave.pagan@oracle.com
To: Gcc Patch List <gcc-patches@gcc.gnu.org>
Cc: Joseph Myers <joseph@codesourcery.com>
Subject: [PATCH] PR c/46921 Lost side effect when struct initializer expression uses comma operator
Date: Tue, 27 Feb 2018 00:05:00 -0000	[thread overview]
Message-ID: <f34d3ae9-ca59-9a6b-db77-69b61e50b852@oracle.com> (raw)

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

This patch fixes improper handling of comma operator expression in a 
struct field initializer as described in:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46921

Currently, function output_init_element () does not evaluate the left 
hand expression in a comma operator that's used for a struct initializer 
field if the right hand side is zero-sized. However, the left hand 
expression must be evaluated if it's found to have side effects (for 
example, a function call).

Patch was successfully bootstrapped and tested on x86_64-linux.

--Dave



[-- Attachment #2: CL-46921 --]
[-- Type: text/plain, Size: 292 bytes --]

/c
2018-02-26  David Pagan  <dave.pagan@oracle.com>

	PR c/46921
	* c-typeck.c (output_init_element): Ensure field initializer 
	expression is always evaluated if there are side effects.

/testsuite
2018-02-26  David Pagan  <dave.pagan@oracle.com>

	PR c/46921
	* gcc.dg/pr46921.c: New test.

[-- Attachment #3: PATCH-46921 --]
[-- Type: text/plain, Size: 1889 bytes --]

Index: gcc/c/c-typeck.c
===================================================================
--- gcc/c/c-typeck.c	(revision 257975)
+++ gcc/c/c-typeck.c	(working copy)
@@ -9208,12 +9208,14 @@ output_init_element (location_t loc, tree value, t
 		      "enum conversion in initialization is invalid in C++");
     }
 
-  /* If this field is empty (and not at the end of structure),
-     don't do anything other than checking the initializer.  */
+  /* If this field is empty and does not have side effects (and is not at 
+     the end of structure), don't do anything other than checking the 
+     initializer.  */
   if (field
       && (TREE_TYPE (field) == error_mark_node
 	  || (COMPLETE_TYPE_P (TREE_TYPE (field))
 	      && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
+	      && !TREE_SIDE_EFFECTS (new_value)
 	      && (TREE_CODE (constructor_type) == ARRAY_TYPE
 		  || DECL_CHAIN (field)))))
     return;
Index: gcc/testsuite/gcc.dg/pr46921.c
===================================================================
--- gcc/testsuite/gcc.dg/pr46921.c	(revision 0)
+++ gcc/testsuite/gcc.dg/pr46921.c	(working copy)
@@ -0,0 +1,40 @@
+/* PR c/46921  lost side effect when struct initializer expr uses comma
+      operator  */
+
+/* { dg-do run } */
+/* { dg-options "" } */
+
+extern int printf(const char *, ...);
+extern void abort (void);
+
+typedef struct __uws_0 { } uw_unit;
+uw_unit uw_unit_v = {};
+
+struct __uws_1 
+{
+  struct __uws_0 __uwf_1;
+  struct __uws_1* __uwf_2;
+};
+
+static int left_hand_eval = 0;
+
+static void
+foo (const char *s)
+{
+  ++left_hand_eval;
+  printf("%s", s);
+}
+
+int
+main ()
+{
+  struct __uws_1 tmp = {(foo("Inner\n"), uw_unit_v)};
+  
+  printf("Outer\n");
+  /* left hand expression in comma operator initializer must always be
+     evaluated if there are side effects.  */
+  if (!left_hand_eval)
+    abort ();
+  
+  return 0;
+}

             reply	other threads:[~2018-02-27  0:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-27  0:05 dave.pagan [this message]
2018-03-13 18:23 ` Joseph Myers

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=f34d3ae9-ca59-9a6b-db77-69b61e50b852@oracle.com \
    --to=dave.pagan@oracle.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.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).