public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marek Polacek <polacek@redhat.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>,
	       Joseph Myers <joseph@codesourcery.com>,
	       David Malcolm <dmalcolm@redhat.com>
Subject: C PATCH to fix ICE with -Wformat and zero-length array (PR c/80919)
Date: Fri, 02 Jun 2017 17:19:00 -0000	[thread overview]
Message-ID: <20170602171912.GH3413@redhat.com> (raw)

In the C FE, zero-length arrays require structural equality, so we can't
compare their canonical types, 'cause they're NULL.  But matching_type_p didn't
know that so we were crashing.  With this patch the ICE is gone and the warning
messages are the same as with e.g. "int a[1]".

Bootstrapped/regtested on x86_64-linux, ok for trunk/7?

2017-06-02  Marek Polacek  <polacek@redhat.com>

	PR c/80919
	* c-format.c (matching_type_p): Return false if any of the types
	requires structural equality.

	* gcc.dg/format/pr80919.c: New test.

diff --git gcc/c-family/c-format.c gcc/c-family/c-format.c
index faef267..732339b 100644
--- gcc/c-family/c-format.c
+++ gcc/c-family/c-format.c
@@ -3278,6 +3278,12 @@ matching_type_p (tree spec_type, tree arg_type)
   gcc_assert (spec_type);
   gcc_assert (arg_type);
 
+  /* If any of the types requires structural equality, we can't compare
+     their canonical types.  */
+  if (TYPE_STRUCTURAL_EQUALITY_P (spec_type)
+      || TYPE_STRUCTURAL_EQUALITY_P (arg_type))
+    return false;
+
   spec_type = TYPE_CANONICAL (spec_type);
   arg_type = TYPE_CANONICAL (arg_type);
 
diff --git gcc/testsuite/gcc.dg/format/pr80919.c gcc/testsuite/gcc.dg/format/pr80919.c
index e69de29..510c2e4 100644
--- gcc/testsuite/gcc.dg/format/pr80919.c
+++ gcc/testsuite/gcc.dg/format/pr80919.c
@@ -0,0 +1,16 @@
+/* PR c/80919 */
+/* { dg-do compile } */
+/* { dg-options "-Wall" } */
+
+void
+fn (void)
+{
+  int a[0];
+  __builtin_printf("%d\n", &a); /* { dg-warning "expects argument of type" } */
+  __builtin_printf("%i\n", &a); /* { dg-warning "expects argument of type" } */
+
+  __builtin_printf("%o\n", &a); /* { dg-warning "expects argument of type" } */
+  __builtin_printf("%u\n", &a); /* { dg-warning "expects argument of type" } */
+  __builtin_printf("%x\n", &a); /* { dg-warning "expects argument of type" } */
+  __builtin_printf("%X\n", &a); /* { dg-warning "expects argument of type" } */
+}

	Marek

             reply	other threads:[~2017-06-02 17:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-02 17:19 Marek Polacek [this message]
2017-06-02 19:51 ` 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=20170602171912.GH3413@redhat.com \
    --to=polacek@redhat.com \
    --cc=dmalcolm@redhat.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).