[PATCH] c-family: Fix ICE in get_atomic_generic_size [PR96545]

Jakub Jelinek jakub@redhat.com
Tue Aug 11 08:50:28 GMT 2020


Hi!

As the testcase shows, we would ICE if the type of the first argument of
various atomic builtins was pointer to (non-void) incomplete type, we would
assume that TYPE_SIZE_UNIT must be non-NULL.  This patch diagnoses it
instead.  And also changes the TREE_CODE != INTEGER_CST check to
!tree_fits_uhwi_p, as we use tree_to_uhwi after this and at least in theory
the int could be too large and not fit.

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

2020-08-10  Jakub Jelinek  <jakub@redhat.com>

	PR c/96545
	* c-common.c (get_atomic_generic_size): Require that first argument's
	type points to a complete type and use tree_fits_uhwi_p instead of
	just INTEGER_CST TREE_CODE check for the TYPE_SIZE_UNIT.

	* c-c++-common/pr96545.c: New test.

--- gcc/c-family/c-common.c.jj	2020-07-31 23:07:00.566153515 +0200
+++ gcc/c-family/c-common.c	2020-08-10 12:03:35.236841534 +0200
@@ -7017,8 +7017,15 @@ get_atomic_generic_size (location_t loc,
       return 0;
     }
 
+  if (!COMPLETE_TYPE_P (TREE_TYPE (type_0)))
+    {
+      error_at (loc, "argument 1 of %qE must be a pointer to a complete type",
+		function);
+      return 0;
+    }
+
   /* Types must be compile time constant sizes. */
-  if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
+  if (!tree_fits_uhwi_p ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))))
     {
       error_at (loc, 
 		"argument 1 of %qE must be a pointer to a constant size type",
--- gcc/testsuite/c-c++-common/pr96545.c.jj	2020-08-10 12:28:43.296222401 +0200
+++ gcc/testsuite/c-c++-common/pr96545.c	2020-08-10 12:28:28.258428487 +0200
@@ -0,0 +1,31 @@
+/* PR c/96545 */
+/* { dg-do compile } */
+
+extern char x[], y[], z[];
+struct S;
+extern struct S s, t, u;
+int v, w;
+
+void
+foo (void)
+{
+  __atomic_exchange (&x, &y, &z, 0);	/* { dg-error "must be a pointer to a complete type" } */
+}
+
+void
+bar (void)
+{
+  __atomic_exchange (&s, &t, &u, 0);	/* { dg-error "must be a pointer to a complete type" } */
+}
+
+void
+baz (void)
+{
+  __atomic_exchange (&v, &t, &w, 0);	/* { dg-error "size mismatch in argument 2 of" } */
+}
+
+void
+qux (void)
+{
+  __atomic_exchange (&v, &w, &t, 0);	/* { dg-error "size mismatch in argument 3 of" } */
+}

	Jakub



More information about the Gcc-patches mailing list