From: Fritz Reese Date: Tue, 14 Nov 2017 01:25:26 +0000 (+0000) Subject: re PR fortran/78240 (ICE in match_clist_expr, at fortran/decl.c:728) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=9b24c104aeb9dcaa06c0fe1324b8d9ef1fc2d92a;p=gcc.git re PR fortran/78240 (ICE in match_clist_expr, at fortran/decl.c:728) 2017-11-13 Fritz Reese PR fortran/78240 gcc/fortran/ChangeLog: PR fortran/78240 * decl.c (match_clist_expr): Replace gcc_assert with proper handling of bad result from spec_size(). * resolve.c (check_data_variable): Avoid NULL dereference when passing locus to gfc_error. gcc/testsuite/ChangeLog: PR fortran/78240 * gfortran.dg/dec_structure_23.f90: New. * gfortran.dg/pr78240.f90: New. From-SVN: r254718 --- diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index d766a4e56b6..54877ff50a9 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,11 @@ +2017-11-13 Fritz Reese + + PR fortran/78240 + * decl.c (match_clist_expr): Replace gcc_assert with proper + handling of bad result from spec_size(). + * resolve.c (check_data_variable): Avoid NULL dereference when passing + locus to gfc_error. + 2017-11-11 Janus Weil PR fortran/82932 diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c index 11264e757e6..e57cfded540 100644 --- a/gcc/fortran/decl.c +++ b/gcc/fortran/decl.c @@ -632,14 +632,13 @@ match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as) gfc_expr *expr = NULL; match m; locus where; - mpz_t repeat, size; + mpz_t repeat, cons_size, as_size; bool scalar; int cmp; gcc_assert (ts); mpz_init_set_ui (repeat, 0); - mpz_init (size); scalar = !as || !as->rank; /* We have already matched '/' - now look for a constant list, as with @@ -733,16 +732,30 @@ match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as) expr->rank = as->rank; expr->shape = gfc_get_shape (expr->rank); - /* Validate sizes. */ - gcc_assert (gfc_array_size (expr, &size)); - gcc_assert (spec_size (as, &repeat)); - cmp = mpz_cmp (size, repeat); - if (cmp < 0) - gfc_error ("Not enough elements in array initializer at %C"); - else if (cmp > 0) - gfc_error ("Too many elements in array initializer at %C"); + /* Validate sizes. We built expr ourselves, so cons_size will be + constant (we fail above for non-constant expressions). + We still need to verify that the array-spec has constant size. */ + cmp = 0; + gcc_assert (gfc_array_size (expr, &cons_size)); + if (!spec_size (as, &as_size)) + { + gfc_error ("Expected constant array-spec in initializer list at %L", + as->type == AS_EXPLICIT ? &as->upper[0]->where : &where); + cmp = -1; + } + else + { + /* Make sure the specs are of the same size. */ + cmp = mpz_cmp (cons_size, as_size); + if (cmp < 0) + gfc_error ("Not enough elements in array initializer at %C"); + else if (cmp > 0) + gfc_error ("Too many elements in array initializer at %C"); + mpz_clear (as_size); + } + mpz_clear (cons_size); if (cmp) - goto cleanup; + goto cleanup; } /* Make sure scalar types match. */ @@ -754,7 +767,6 @@ match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as) expr->ts.u.cl->length_from_typespec = 1; *result = expr; - mpz_clear (size); mpz_clear (repeat); return MATCH_YES; @@ -766,7 +778,6 @@ cleanup: expr->value.constructor = NULL; gfc_free_expr (expr); gfc_constructor_free (array_head); - mpz_clear (size); mpz_clear (repeat); return MATCH_ERROR; } diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c index 28a0c9e74b0..bdb4015b34d 100644 --- a/gcc/fortran/resolve.c +++ b/gcc/fortran/resolve.c @@ -15286,7 +15286,7 @@ check_data_variable (gfc_data_variable *var, locus *where) if (!gfc_array_size (e, &size)) { gfc_error ("Nonconstant array section at %L in DATA statement", - &e->where); + where); mpz_clear (offset); return false; } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e5226149996..3c4c37d517b 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2017-11-13 Fritz Reese + + PR fortran/78240 + * gfortran.dg/dec_structure_23.f90: New. + * gfortran.dg/pr78240.f90: New. + 2017-11-13 Carl Love * gcc.target/powerpc/builtin-vec-sums-be-int.c: New test file. diff --git a/gcc/testsuite/gfortran.dg/dec_structure_23.f90 b/gcc/testsuite/gfortran.dg/dec_structure_23.f90 new file mode 100644 index 00000000000..3c68489c4bd --- /dev/null +++ b/gcc/testsuite/gfortran.dg/dec_structure_23.f90 @@ -0,0 +1,19 @@ +! { dg-do compile } +! { dg-options "-fdec-structure" } +! +! PR fortran/78240 +! +! Test a regression where an ICE occurred attempting to create array variables +! with non-constant array-specs in legacy clist initializers. +! + +program p + implicit none + integer :: nn + real :: rr + structure /s/ + integer x(n) /1/ ! { dg-error "xpected constant" } + integer xx(nn) /1/ ! { dg-error "xpected constant" } + integer xxx(rr) /1.0/ ! { dg-error "xpected constant" } + end structure +end diff --git a/gcc/testsuite/gfortran.dg/pr78240.f90 b/gcc/testsuite/gfortran.dg/pr78240.f90 new file mode 100644 index 00000000000..76542bfbab2 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr78240.f90 @@ -0,0 +1,12 @@ +! { dg-do compile } +! +! PR fortran/78240 +! +! Test a regression where an ICE occurred by passing an invalid reference +! to the error handling routine for non-constant array-specs in DATA list +! initializers. +! + +program p + integer x(n) /1/ ! { dg-error "Nonconstant array" } +end