public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C PATCH to fix ICE with -Wformat and zero-length array (PR c/80919)
@ 2017-06-02 17:19 Marek Polacek
  2017-06-02 19:51 ` Joseph Myers
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2017-06-02 17:19 UTC (permalink / raw)
  To: GCC Patches, Joseph Myers, David Malcolm

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-06-02 19:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-02 17:19 C PATCH to fix ICE with -Wformat and zero-length array (PR c/80919) Marek Polacek
2017-06-02 19:51 ` Joseph Myers

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