]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/cp/init.c
PR c++/87996 - size of array is negative error when SIZE_MAX/2 < sizeof(array) <...
[thirdparty/gcc.git] / gcc / cp / init.c
index efbda3a13a68d37e5600ffd253a6b2843082a3f8..606d246ef940bb24298b01c8f95b5e7e01b1e971 100644 (file)
@@ -3086,7 +3086,21 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
       if (overflow || wi::gtu_p (inner_size, max_size))
        {
          if (complain & tf_error)
-           error ("size of array is too large");
+           {
+             cst_size_error error;
+             if (overflow)
+               error = cst_size_overflow;
+             else
+               {
+                 error = cst_size_too_big;
+                 size = size_binop (MULT_EXPR, size,
+                                    wide_int_to_tree (sizetype,
+                                                      inner_nelts_count));
+                 size = cp_fully_fold (size);
+               }
+             invalid_array_size_error (input_location, error, size,
+                                       /*name=*/NULL_TREE);
+           }
          return error_mark_node;
        }
 
@@ -3105,7 +3119,11 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
                 isn't explicitly stated but it's enforced anyway -- see
                 grokdeclarator in cp/decl.c).  */
              if (complain & tf_error)
-               error ("size of array is too large");
+               {
+                 size = cp_fully_fold (size);
+                 invalid_array_size_error (input_location, cst_size_too_big,
+                                           size, NULL_TREE);
+               }
              return error_mark_node;
            }
        }
@@ -3747,12 +3765,9 @@ build_new (vec<tree, va_gc> **placement, tree type, tree nelts,
         less than zero. ... If the expression is a constant expression,
         the program is ill-fomed.  */
       if (TREE_CODE (cst_nelts) == INTEGER_CST
-         && tree_int_cst_sgn (cst_nelts) == -1)
-       {
-         if (complain & tf_error)
-           error ("size of array is negative");
-         return error_mark_node;
-       }
+         && !valid_array_size_p (input_location, cst_nelts, NULL_TREE,
+                                 complain & tf_error))
+       return error_mark_node;
 
       nelts = mark_rvalue_use (nelts);
       nelts = cp_save_expr (cp_convert (sizetype, nelts, complain));