]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
backport: re PR c++/77722 (-fsanitize=undefined doesn't give runtime error in functio...
authorJakub Jelinek <jakub@redhat.com>
Tue, 30 May 2017 07:31:03 +0000 (09:31 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 30 May 2017 07:31:03 +0000 (09:31 +0200)
Backported from mainline
2016-09-27  Jakub Jelinek  <jakub@redhat.com>

PR c++/77722
* cp-gimplify.c (cp_ubsan_maybe_instrument_return): Instrument also
functions that have just a STATEMENT_LIST instead of BIND_EXPR, or
BIND_EXPR with some statement rather than STATEMENT_LIST as body.

* g++.dg/ubsan/return-4.C: New test.
* g++.dg/ubsan/return-5.C: New test.
* g++.dg/ubsan/return-6.C: New test.

From-SVN: r248614

gcc/cp/ChangeLog
gcc/cp/cp-gimplify.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ubsan/return-4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ubsan/return-5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ubsan/return-6.C [new file with mode: 0644]

index 7770101444ea78bb39af3701aa3c08756ec84bf5..9a2f36cc64416c4a8117ed3307d26a47af00bb65 100644 (file)
@@ -1,6 +1,13 @@
 2017-05-30  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2016-09-27  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/77722
+       * cp-gimplify.c (cp_ubsan_maybe_instrument_return): Instrument also
+       functions that have just a STATEMENT_LIST instead of BIND_EXPR, or
+       BIND_EXPR with some statement rather than STATEMENT_LIST as body.
+
        2016-09-20  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/77637
index 41b34185425f4c04681e38d7a0619ef6a9b39fbd..b948c536d3f05435e469534f2d31a98e56807aa1 100644 (file)
@@ -1337,14 +1337,11 @@ cp_ubsan_maybe_instrument_return (tree fndecl)
     }
   if (t == NULL_TREE)
     return;
-  t = DECL_SAVED_TREE (fndecl);
-  if (TREE_CODE (t) == BIND_EXPR
-      && TREE_CODE (BIND_EXPR_BODY (t)) == STATEMENT_LIST)
-    {
-      tree_stmt_iterator i = tsi_last (BIND_EXPR_BODY (t));
-      t = ubsan_instrument_return (DECL_SOURCE_LOCATION (fndecl));
-      tsi_link_after (&i, t, TSI_NEW_STMT);
-    }
+  tree *p = &DECL_SAVED_TREE (fndecl);
+  if (TREE_CODE (*p) == BIND_EXPR)
+    p = &BIND_EXPR_BODY (*p);
+  t = ubsan_instrument_return (DECL_SOURCE_LOCATION (fndecl));
+  append_to_statement_list (t, p);
 }
 
 void
index 092fd3c709a3fe387ec3e0341f3c0b61e17eccc5..41937cdaee436ebcc8da60023fc21187e562de7a 100644 (file)
@@ -3,6 +3,11 @@
        Backported from mainline
        2016-09-27  Jakub Jelinek  <jakub@redhat.com>
 
+       PR c++/77722
+       * g++.dg/ubsan/return-4.C: New test.
+       * g++.dg/ubsan/return-5.C: New test.
+       * g++.dg/ubsan/return-6.C: New test.
+
        PR fortran/77666
        * gfortran.dg/gomp/pr77666.f90: New test.
 
diff --git a/gcc/testsuite/g++.dg/ubsan/return-4.C b/gcc/testsuite/g++.dg/ubsan/return-4.C
new file mode 100644 (file)
index 0000000..d30eef8
--- /dev/null
@@ -0,0 +1,18 @@
+// PR c++/77722
+// { dg-do run }
+// { dg-options "-fsanitize=return -w" }
+// { dg-shouldfail "ubsan" }
+
+int
+foo ()
+{
+}
+
+int
+main ()
+{
+  foo ();
+  return 0;
+}
+
+// { dg-output "execution reached the end of a value-returning function without returning a value" }
diff --git a/gcc/testsuite/g++.dg/ubsan/return-5.C b/gcc/testsuite/g++.dg/ubsan/return-5.C
new file mode 100644 (file)
index 0000000..2956c33
--- /dev/null
@@ -0,0 +1,19 @@
+// PR c++/77722
+// { dg-do run }
+// { dg-options "-fsanitize=return -w" }
+// { dg-shouldfail "ubsan" }
+
+int
+foo ()
+{
+  int a = 5;
+}
+
+int
+main ()
+{
+  foo ();
+  return 0;
+}
+
+// { dg-output "execution reached the end of a value-returning function without returning a value" }
diff --git a/gcc/testsuite/g++.dg/ubsan/return-6.C b/gcc/testsuite/g++.dg/ubsan/return-6.C
new file mode 100644 (file)
index 0000000..0c1e792
--- /dev/null
@@ -0,0 +1,20 @@
+// PR c++/77722
+// { dg-do run }
+// { dg-options "-fsanitize=return -w" }
+// { dg-shouldfail "ubsan" }
+
+int
+foo ()
+{
+  int a = 5;
+  int b = 5;
+}
+
+int
+main ()
+{
+  foo ();
+  return 0;
+}
+
+// { dg-output "execution reached the end of a value-returning function without returning a value" }