Bug 31304 - REPEAT argument NCOPIES is not converted as it should
Summary: REPEAT argument NCOPIES is not converted as it should
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: 4.3.0
Assignee: Francois-Xavier Coudert
URL:
Keywords: accepts-invalid, ice-on-valid-code, patch
Depends on:
Blocks:
 
Reported: 2007-03-21 23:55 UTC by Francois-Xavier Coudert
Modified: 2007-04-03 22:07 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.3.0
Known to fail: 4.2.0 4.1.3
Last reconfirmed: 2007-03-21 23:55:36


Attachments
Patch that fixes the PR and a few related issues (2.77 KB, patch)
2007-03-31 12:29 UTC, Francois-Xavier Coudert
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Francois-Xavier Coudert 2007-03-21 23:55:00 UTC
The NCOPIES argument of REPEAT is not converted to the integer king the library expects (kind=4), which can lead to segfaults. I changed that code recently, so I'll fix it. I'll probably take this opportunity to have the front-end actually emit an error if the NCOPIES argument is larger than a fixed value.

$ cat repeat.f90
   implicit none

   integer(kind=1) i
   real(kind=8) r
   character(len=2) s1, s2

   i = 1
   r = 1
   s1 = '42'
   r = nearest(r,r)
   s2 = repeat(s1,i)
end
$ gfortran repeat.f90 && ./a.out
Segmentation fault
Comment 1 Francois-Xavier Coudert 2007-03-30 11:00:06 UTC
We should also diagnostic better the cases of negative of too large NCOPIES argument, for both parameters (in simplification routine) and non-parameters.
Comment 2 Francois-Xavier Coudert 2007-03-31 12:29:21 UTC
Created attachment 13307 [details]
Patch that fixes the PR and a few related issues

This patch fixes both the simplification routine and the code generation. It removes the need for the libgfortran string_repeat function by emitting inline calls to memmove(), which probably allows greater optimization.

I added checking on too large values of the NCOPIES argument: NCOPIES has to be such that the destination length fits in the character length integer kind (which is for now hardcoded as kind=4, but I hope we can change that someday).
Comment 3 Francois-Xavier Coudert 2007-04-03 22:05:24 UTC
Subject: Bug 31304

Author: fxcoudert
Date: Tue Apr  3 22:05:14 2007
New Revision: 123481

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=123481
Log:
	PR fortran/31304

	* fortran/gfortran.h (gfc_charlen_int_kind): New prototype.
	* fortran/trans-types.c (gfc_charlen_int_kind): New variable.
	(gfc_init_types): Define gfc_charlen_int_kind. 
	* fortran/trans.h (gfor_fndecl_string_repeat): Remove prototype.
	* fortran/trans-decl.c (gfor_fndecl_string_repeat): Delete.
	(gfc_build_intrinsic_function_decls): Don't set
	gfor_fndecl_string_repeat.
	* fortran/trans-intrinsic.c (gfc_conv_intrinsic_repeat): Rewrite
	so that we don't have to call a library function.
	* fortran/simplify.c (gfc_simplify_repeat): Perform the necessary
	checks on the NCOPIES argument, and work with arbitrary size
	arguments.

	* intrinsics/string_intrinsics.c (string_repeat): Remove.

	* gfortran.dg/repeat_2.f90: New test.
	* gfortran.dg/repeat_3.f90: New test.
	* gfortran.dg/repeat_4.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/repeat_2.f90
    trunk/gcc/testsuite/gfortran.dg/repeat_3.f90
    trunk/gcc/testsuite/gfortran.dg/repeat_4.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/simplify.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-intrinsic.c
    trunk/gcc/fortran/trans-types.c
    trunk/gcc/fortran/trans.h
    trunk/gcc/testsuite/ChangeLog
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/intrinsics/string_intrinsics.c

Comment 4 Francois-Xavier Coudert 2007-04-03 22:07:05 UTC
Fixed on mainline, closing.