public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-8585] c++: fix SIGFPE with -Wclass-memaccess [PR105634]
@ 2022-07-19 18:25 Marek Polacek
  0 siblings, 0 replies; only message in thread
From: Marek Polacek @ 2022-07-19 18:25 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:deafa40eb57b39626e116ca6a15d34a34c57c9f6

commit r12-8585-gdeafa40eb57b39626e116ca6a15d34a34c57c9f6
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Jul 19 14:24:25 2022 -0400

    c++: fix SIGFPE with -Wclass-memaccess [PR105634]
    
    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.

Diff:
---
 gcc/cp/call.cc                                 |  2 ++
 gcc/testsuite/g++.dg/warn/Wclass-memaccess-7.C | 13 +++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 726770d4db8..af92a961000 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -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
index 00000000000..7e86b248629
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wclass-memaccess-7.C
@@ -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));
+  }
+};


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-19 18:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-19 18:25 [gcc r12-8585] c++: fix SIGFPE with -Wclass-memaccess [PR105634] Marek Polacek

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