diff options
author | Martin Sebor <msebor@redhat.com> | 2019-02-11 17:35:17 +0000 |
---|---|---|
committer | Martin Sebor <msebor@gcc.gnu.org> | 2019-02-11 10:35:17 -0700 |
commit | 28a8cef1e240989c199dfd8538c826c134098f85 (patch) | |
tree | a7b930a4f8e120d187bde8f6ea311c2ed60e6d55 /gcc/cp | |
parent | 3c4860e346b8292ac398d971aff933586b11e3ea (diff) | |
download | gcc-28a8cef1e240989c199dfd8538c826c134098f85.zip gcc-28a8cef1e240989c199dfd8538c826c134098f85.tar.gz gcc-28a8cef1e240989c199dfd8538c826c134098f85.tar.bz2 |
PR c++/87996 - size of array is negative error when SIZE_MAX/2 < sizeof(array) <= SIZE_MAX
gcc/ChangeLog:
PR c++/87996
* builtins.c (max_object_size): Move from here...
* builtins.h (max_object_size): ...and here...
* tree.c (max_object_size): ...to here...
* tree.h (max_object_size): ...and here.
gcc/c-family/ChangeLog:
PR c++/87996
* c-common.c (invalid_array_size_error): New function.
(valid_array_size_p): Call it. Handle size as well as type.
* c-common.h (valid_constant_size_p): New function.
(enum cst_size_error): New type.
gcc/cp/ChangeLog:
PR c++/87996
* decl.c (compute_array_index_type_loc): Preserve signed sizes
for diagnostics. Call valid_array_size_p instead of error.
* init.c (build_new_1): Compute size for diagnostic. Call
invalid_array_size_error
(build_new): Call valid_array_size_p instead of error.
gcc/testsuite/ChangeLog:
PR c++/87996
* c-c++-common/array-5.c: New test.
* c-c++-common/pr68107.c: Adjust text of diagnostics.
* g++.dg/init/new38.C: Same.
* g++.dg/init/new43.C: Same.
* g++.dg/init/new44.C: Same.
* g++.dg/init/new46.C: Same.
* g++.dg/other/large-size-array.C: Same.
* g++.dg/other/new-size-type.C: Same.
* g++.dg/template/array30.C: Same.
* g++.dg/template/array32.C: New test.
* g++.dg/template/dependent-name3.C: Adjust.
* gcc.dg/large-size-array-3.c: Same.
* gcc.dg/large-size-array-5.c: Same.
* gcc.dg/large-size-array.c: Same.
* g++.old-deja/g++.brendan/array1.C: Same.
* g++.old-deja/g++.mike/p6149.C: Same.
From-SVN: r268774
Diffstat (limited to 'gcc/cp')
-rw-r--r-- | gcc/cp/ChangeLog | 9 | ||||
-rw-r--r-- | gcc/cp/decl.c | 42 | ||||
-rw-r--r-- | gcc/cp/init.c | 31 |
3 files changed, 62 insertions, 20 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 3cafaaa..76f22f5 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,12 @@ +2019-02-11 Martin Sebor <msebor@redhat.com> + + PR c++/87996 + * decl.c (compute_array_index_type_loc): Preserve signed sizes + for diagnostics. Call valid_array_size_p instead of error. + * init.c (build_new_1): Compute size for diagnostic. Call + invalid_array_size_error + (build_new): Call valid_array_size_p instead of error. + 2019-02-07 Alexandre Oliva <aoliva@redhat.com> PR c++/86218 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 65ba812..4038197 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -9652,17 +9652,21 @@ static tree compute_array_index_type_loc (location_t name_loc, tree name, tree size, tsubst_flags_t complain) { - tree itype; - tree osize = size; - if (error_operand_p (size)) return error_mark_node; + /* The type of the index being computed. */ + tree itype; + + /* The original numeric size as seen in the source code before + conversion to size_t. */ + tree origsize = size; + location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location); if (!type_dependent_expression_p (size)) { - osize = size = mark_rvalue_use (size); + origsize = size = mark_rvalue_use (size); if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR && TREE_SIDE_EFFECTS (size)) @@ -9679,7 +9683,7 @@ compute_array_index_type_loc (location_t name_loc, tree name, tree size, /*manifestly_const_eval=*/true); if (!TREE_CONSTANT (size)) - size = osize; + size = origsize; } if (error_operand_p (size)) @@ -9740,16 +9744,30 @@ compute_array_index_type_loc (location_t name_loc, tree name, tree size, /* Normally, the array-bound will be a constant. */ if (TREE_CODE (size) == INTEGER_CST) { - /* An array must have a positive number of elements. */ - if (!valid_constant_size_p (size)) + /* The size to use in diagnostics that reflects the constant + size used in the source, rather than SIZE massaged above. */ + tree diagsize = size; + + /* If the original size before conversion to size_t was signed + and negative, convert it to ssizetype to restore the sign. */ + if (!TYPE_UNSIGNED (TREE_TYPE (origsize)) + && TREE_CODE (size) == INTEGER_CST + && tree_int_cst_sign_bit (size)) + { + diagsize = fold_convert (ssizetype, size); + + /* Clear the overflow bit that may have been set as a result + of the conversion from the sizetype of the new size to + ssizetype. */ + TREE_OVERFLOW (diagsize) = false; + } + + /* Verify that the array has a positive number of elements + and issue the appropriate diagnostic if it doesn't. */ + if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error))) { if (!(complain & tf_error)) return error_mark_node; - - if (name) - error_at (loc, "size of array %qD is negative", name); - else - error_at (loc, "size of array is negative"); size = integer_one_node; } /* As an extension we allow zero-sized arrays. */ diff --git a/gcc/cp/init.c b/gcc/cp/init.c index efbda3a..606d246 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -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)); |