c++: fix SIGFPE with -Wclass-memaccess [PR105634]
authorMarek Polacek <polacek@redhat.com>
Tue, 19 Jul 2022 18:24:25 +0000 (14:24 -0400)
committerMarek Polacek <polacek@redhat.com>
Tue, 19 Jul 2022 18:25:31 +0000 (14:25 -0400)
Here we crash because we attempt to % by 0.  Thus fixed.

PR c++/105634

gcc/cp/ChangeLog:

* call.cc (maybe_warn_class_memaccess): Avoid % by zero.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wclass-memaccess-7.C: New test.

gcc/cp/call.cc
gcc/testsuite/g++.dg/warn/Wclass-memaccess-7.C [new file with mode: 0644]

index 726770d..af92a96 100644 (file)
@@ -10333,6 +10333,8 @@ maybe_warn_class_memaccess (location_t loc, tree fndecl,
          /* Finally, warn on partial copies.  */
          unsigned HOST_WIDE_INT typesize
            = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
+         if (typesize == 0)
+           break;
          if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
            warned = warning_at (loc, OPT_Wclass_memaccess,
                                 (typesize - partial > 1
diff --git a/gcc/testsuite/g++.dg/warn/Wclass-memaccess-7.C b/gcc/testsuite/g++.dg/warn/Wclass-memaccess-7.C
new file mode 100644 (file)
index 0000000..7e86b24
--- /dev/null
@@ -0,0 +1,13 @@
+// PR c++/105634
+// { dg-do compile { target { c++11 } } }
+// { dg-options "-Wall" }
+
+struct s
+{
+  struct {} a[] = 1.0; // { dg-error "" }
+  void f (char *c)
+  {
+    s s;
+    __builtin_memcpy (&s, c, sizeof(s));
+  }
+};