diff mbox

C PATCH to fix ICE with -Wformat and zero-length array (PR c/80919)

Message ID 20170602171912.GH3413@redhat.com
State New
Headers show

Commit Message

Marek Polacek June 2, 2017, 5:19 p.m. UTC
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.


	Marek

Comments

Joseph Myers June 2, 2017, 7:50 p.m. UTC | #1
On Fri, 2 Jun 2017, Marek Polacek wrote:

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

OK.
diff mbox

Patch

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" } */
+}