]> gcc.gnu.org Git - gcc.git/commitdiff
re PR middle-end/49905 (Better sanity checking on sprintf src & dest to produce warni...
authorJakub Jelinek <jakub@redhat.com>
Thu, 4 Aug 2011 07:40:24 +0000 (09:40 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Thu, 4 Aug 2011 07:40:24 +0000 (09:40 +0200)
PR middle-end/49905
* tree.h (init_attributes): New prototype.
* attribs.c (init_attributes): No longer static.

* decl.c (cxx_init_decl_processing): Add alloc_size (1) attribute
for operator new and operator new [].  Call init_attributes.

* g++.dg/ext/builtin-object-size3.C: New test.

From-SVN: r177316

gcc/ChangeLog
gcc/attribs.c
gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ext/builtin-object-size3.C [new file with mode: 0644]
gcc/tree.h

index 5449bf33f8dccb74a0cb62313eff42bc2f37c25f..03975637f439f680edad1445030f002564cba015 100644 (file)
@@ -1,3 +1,9 @@
+2011-08-04  Jakub Jelinek  <jakub@redhat.com>
+
+       PR middle-end/49905
+       * tree.h (init_attributes): New prototype.
+       * attribs.c (init_attributes): No longer static.
+
 2011-08-04  Ramana Radhakrishnan  <ramana.radhakrishnan@linaro.org>
 
        * config/arm/arm.c (arm_set_fixed_optab_libfunc): Constify
index 98a6310294bba6b08e6a7714116dd75c42a13a43..9448c0c6beb5bb0cee623fcaec4b452b789a2ab9 100644 (file)
@@ -34,8 +34,6 @@ along with GCC; see the file COPYING3.  If not see
 #include "hashtab.h"
 #include "plugin.h"
 
-static void init_attributes (void);
-
 /* Table of the tables of attributes (common, language, format, machine)
    searched.  */
 static const struct attribute_spec *attribute_tables[4];
@@ -107,12 +105,15 @@ eq_attr (const void *p, const void *q)
 /* Initialize attribute tables, and make some sanity checks
    if --enable-checking.  */
 
-static void
+void
 init_attributes (void)
 {
   size_t i;
   int k;
 
+  if (attributes_initialized)
+    return;
+
   attribute_tables[0] = lang_hooks.common_attribute_table;
   attribute_tables[1] = lang_hooks.attribute_table;
   attribute_tables[2] = lang_hooks.format_attribute_table;
index 1c50f0424eec8b07249e7203eb9151b22a8fd44f..9e2a01d8acda0a2fc479e7e8c213b4fcc1fe9c93 100644 (file)
@@ -1,3 +1,9 @@
+2011-08-04  Jakub Jelinek  <jakub@redhat.com>
+
+       PR middle-end/49905
+       * decl.c (cxx_init_decl_processing): Add alloc_size (1) attribute
+       for operator new and operator new [].  Call init_attributes.
+
 2011-08-02  Jason Merrill  <jason@redhat.com>
 
        PR c++/43886
index 2000bd4a79d632872cba12e29d5872747611c136..f0d1a1a326258e5f1b81590155c5b043decc3a4b 100644 (file)
@@ -3629,6 +3629,7 @@ cxx_init_decl_processing (void)
   current_lang_name = lang_name_cplusplus;
 
   {
+    tree newattrs;
     tree newtype, deltype;
     tree ptr_ftype_sizetype;
     tree new_eh_spec;
@@ -3656,7 +3657,13 @@ cxx_init_decl_processing (void)
     else
       new_eh_spec = noexcept_false_spec;
 
-    newtype = build_exception_variant (ptr_ftype_sizetype, new_eh_spec);
+    /* Ensure attribs.c is initialized.  */
+    init_attributes ();
+    newattrs
+      = build_tree_list (get_identifier ("alloc_size"),
+                        build_tree_list (NULL_TREE, integer_one_node));
+    newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
+    newtype = build_exception_variant (newtype, new_eh_spec);
     deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
     push_cp_library_fn (NEW_EXPR, newtype);
     push_cp_library_fn (VEC_NEW_EXPR, newtype);
index 92b1b8d5c45bc9ad3953ac2e0a063dede982c3d0..9565b3e4cfcbd0b0e6e45090fddb3e3f0e377f99 100644 (file)
@@ -1,3 +1,8 @@
+2011-08-04  Jakub Jelinek  <jakub@redhat.com>
+
+       PR middle-end/49905
+       * g++.dg/ext/builtin-object-size3.C: New test.
+
 2011-08-03  Jakub Jelinek  <jakub@redhat.com>
 
        PR tree-optimization/49948
diff --git a/gcc/testsuite/g++.dg/ext/builtin-object-size3.C b/gcc/testsuite/g++.dg/ext/builtin-object-size3.C
new file mode 100644 (file)
index 0000000..09263e5
--- /dev/null
@@ -0,0 +1,26 @@
+// { dg-do compile }
+// { dg-options "-O2" }
+
+void baz (int *, int *);
+
+#define MEMCPY(d,s,l) __builtin___memcpy_chk (d, s, l, __builtin_object_size (d, 0))
+
+int
+foo ()
+{
+  int *p = new int;
+  int *q = new int[4];
+  MEMCPY (p, "abcdefghijklmnopqrstuvwxyz", sizeof (int));
+  MEMCPY (q, "abcdefghijklmnopqrstuvwxyz", 4 * sizeof (int));
+  baz (p, q);
+}
+
+int
+bar ()
+{
+  int *p = new int;
+  int *q = new int[4];
+  MEMCPY (p, "abcdefghijklmnopqrstuvwxyz", sizeof (int) + 1);          // { dg-warning "will always overflow destination buffer" }
+  MEMCPY (q, "abcdefghijklmnopqrstuvwxyz", 4 * sizeof (int) + 1);      // { dg-warning "will always overflow destination buffer" }
+  baz (p, q);
+}
index 6b13a2a5c51e05628690b87168f453cf6f368d2a..367c7b92106fd95e49e149776051c17e239b4589 100644 (file)
@@ -5547,6 +5547,8 @@ extern bool must_pass_in_stack_var_size_or_pad (enum machine_mode, const_tree);
 
 extern const struct attribute_spec *lookup_attribute_spec (const_tree);
 
+extern void init_attributes (void);
+
 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
    which is either a DECL (including a TYPE_DECL) or a TYPE.  If a DECL,
    it should be modified in place; if a TYPE, a copy should be created
This page took 0.145824 seconds and 5 git commands to generate.