From: Jerry DeLisle <jvdelisle@charter.net>
To: gfortran <fortran@gcc.gnu.org>, gcc patches <gcc-patches@gcc.gnu.org>
Subject: [patch, fortran] PR66310 Problems with intrinsic repeat for large number of copies
Date: Tue, 12 Jul 2016 00:54:00 -0000 [thread overview]
Message-ID: <2ea5e1c6-a6cb-f3eb-074d-34584d5e6dfe@charter.net> (raw)
Attached patch sets a limit of huge-1 on the character count to avoid the
integer wrap.
Regression tested with adjustment to test case.
OK for trunk?
Regards,
Jerry
2016-07-11 Jerry DeLisle <jvdelisle@gcc.gnu.org>
PR fortran/66310
* simplify.c (gfc_simplify_repeat): Set max repeat to huge - 1 to allow
one byte for null terminating the resulting string constant.
diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index 95a8d10..03257ec 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -5081,22 +5081,27 @@ gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
/* Check that NCOPIES isn't too large. */
if (len)
{
- mpz_t max, mlen;
+ mpz_t max, mlen, limit;
int i;
- /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
+ /* Compute the maximum value allowed for NCOPIES:
+ huge(cl) - 1 / len. */
mpz_init (max);
+ mpz_init (limit);
i = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
+ /* Set the limit on size to huge-1 to avoid unsigned integer
+ wrapping in gfc_get_character_expr. */
+ mpz_sub_ui (limit, gfc_integer_kinds[i].huge, 1);
+
if (have_length)
{
- mpz_tdiv_q (max, gfc_integer_kinds[i].huge,
- e->ts.u.cl->length->value.integer);
+ mpz_tdiv_q (max, limit, e->ts.u.cl->length->value.integer);
}
else
{
mpz_init_set_si (mlen, len);
- mpz_tdiv_q (max, gfc_integer_kinds[i].huge, mlen);
+ mpz_tdiv_q (max, limit, mlen);
mpz_clear (mlen);
}
@@ -5104,6 +5109,7 @@ gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
if (mpz_cmp (ncopies, max) > 0)
{
mpz_clear (max);
+ mpz_clear (limit);
mpz_clear (ncopies);
gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
&n->where);
@@ -5111,6 +5117,7 @@ gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
}
mpz_clear (max);
+ mpz_clear (limit);
}
mpz_clear (ncopies);
diff --git a/gcc/testsuite/gfortran.dg/repeat_4.f90
b/gcc/testsuite/gfortran.dg/repeat_4.f90
index e5b5acc..77483a1 100644
--- a/gcc/testsuite/gfortran.dg/repeat_4.f90
+++ b/gcc/testsuite/gfortran.dg/repeat_4.f90
@@ -22,7 +22,8 @@ program test
! Check for too large NCOPIES argument and limit cases
print *, repeat(t0, huge(0))
- print *, repeat(t1, huge(0))
+ print *, repeat(t1, huge(0)) ! { dg-error "Argument NCOPIES of REPEAT
intrinsic is too large " }
+ print *, repeat(t1, huge(0) - 1)
print *, repeat(t2, huge(0)) ! { dg-error "Argument NCOPIES of REPEAT
intrinsic is too large " }
print *, repeat(s2, huge(0)) ! { dg-error "Argument NCOPIES of REPEAT
intrinsic is too large " }
next reply other threads:[~2016-07-12 0:54 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-12 0:54 Jerry DeLisle [this message]
2016-07-12 12:57 ` FX
2016-07-13 1:54 ` William Clodius
2016-07-13 3:22 ` Jerry DeLisle
2016-07-12 13:26 Dominique d'Humières
2016-07-12 18:34 ` Jerry DeLisle
2016-07-18 0:03 ` Jerry DeLisle
2016-07-19 9:56 ` Dominique d'Humières
2016-07-19 16:43 ` Jerry DeLisle
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2ea5e1c6-a6cb-f3eb-074d-34584d5e6dfe@charter.net \
--to=jvdelisle@charter.net \
--cc=fortran@gcc.gnu.org \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).