]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: new-expr clobber of constant-size array
authorJason Merrill <jason@redhat.com>
Sat, 4 Oct 2025 08:24:29 +0000 (09:24 +0100)
committerJason Merrill <jason@redhat.com>
Tue, 7 Oct 2025 18:34:54 +0000 (19:34 +0100)
I previously tried to clobber an array as a whole, but fell back on a loop
due to issues with std::construct_at following the resolution of LWG3436.
But the loop seems to make life hard for the optimizers and it occurs to me
that for a one-element array we can just clobber the element type.

This also fixes some xfails in Warray-bounds-20.C.

gcc/cp/ChangeLog:

* init.cc (build_new_1): Clobber a constant-bound array as a whole.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Warray-bounds-20.C: Remove xfails, add diags.

gcc/cp/init.cc
gcc/testsuite/g++.dg/warn/Warray-bounds-20.C

index 3fe476d7eec7343eac36be8aae50b457026f8de0..912298728acd5dff070f531bc8b13ee6b57ba322 100644 (file)
@@ -3662,11 +3662,11 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
   tree clobber_expr = NULL_TREE;
   if (do_clobber)
     {
-      tree clobber = build_clobber (elt_type, CLOBBER_OBJECT_BEGIN);
-      CONSTRUCTOR_IS_DIRECT_INIT (clobber) = true;
-      if (array_p)
+      if (array_p && TREE_CODE (cst_outer_nelts) != INTEGER_CST)
        {
          /* Clobber each element rather than the array at once.  */
+         tree clobber = build_clobber (elt_type, CLOBBER_OBJECT_BEGIN);
+         CONSTRUCTOR_IS_DIRECT_INIT (clobber) = true;
          tree maxindex = cp_build_binary_op (input_location,
                                              MINUS_EXPR, outer_nelts,
                                              integer_one_node,
@@ -3677,7 +3677,25 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
        }
       else
        {
-         tree targ = cp_build_fold_indirect_ref (data_addr);
+         tree targ = data_addr;
+         tree ttype = type;
+         /* Clobber the array as a whole, except that for a one-element array
+            just clobber the element type, to avoid problems with code like
+            construct_at that uses new T[1] for array T to get a pointer to
+            the array.  */
+         if (array_p && !integer_onep (cst_outer_nelts))
+           {
+             tree dom
+               = compute_array_index_type (NULL_TREE,
+                                           CONST_CAST_TREE (cst_outer_nelts),
+                                           complain);
+             ttype = build_cplus_array_type (type, dom);
+             tree ptype = build_pointer_type (ttype);
+             targ = fold_convert (ptype, targ);
+           }
+         targ = cp_build_fold_indirect_ref (targ);
+         tree clobber = build_clobber (ttype, CLOBBER_OBJECT_BEGIN);
+         CONSTRUCTOR_IS_DIRECT_INIT (clobber) = true;
          clobber_expr = cp_build_init_expr (targ, clobber);
        }
     }
index 643e8014d36ed3129615928c30c27636b85c8117..a57499dd07ae790042bf82873f090f281f4c098b 100644 (file)
@@ -53,18 +53,18 @@ void warn_derived_ctor_access_new_alloc ()
 
 void warn_derived_ctor_access_new_array_decl ()
 {
-  char b[sizeof (D1) * 2];    // { dg-message "at offset \\d+ into object 'b' of size 80" "LP64 note" { target { lp64 } xfail { lp64 } } }
-                              // { dg-message "at offset \\d+ into object 'b' of size 40" "LP32 note" { target { ilp32 } xfail { ilp32 } } .-1 }
+  char b[sizeof (D1) * 2];    // { dg-message "at offset \\d+ into object 'b' of size 80" "LP64 note" { target { lp64 } } }
+                              // { dg-message "at offset \\d+ into object 'b' of size 40" "LP64 note" { target { ilp32 } } .-1 }
   char *p = b;
   ++p;
-  D1 *q = new (p) D1[2];
+  D1 *q = new (p) D1[2];       // { dg-message "partly outside array bounds" }
   sink (q);
 }
 
 void warn_derived_ctor_access_new_array_alloc ()
 {
-  char *p = new char[sizeof (D1) * 2];            // { dg-message "at offset \\d+ into object of size \\d+ allocated by '\[^\n\r]*operator new\[^\n\r]*" "note" { xfail *-*-* } }
+  char *p = new char[sizeof (D1) * 2];            // { dg-message "at offset \\d+ into object of size \\d+ allocated by '\[^\n\r]*operator new\[^\n\r]*" "note" }
   ++p;
-  D1 *q = new (p) D1[2];
+  D1 *q = new (p) D1[2];       // { dg-message "partly outside array bounds" }
   sink (q);
 }