Bug 31258 - segfault with transpose(reshape(char))
Summary: segfault with transpose(reshape(char))
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords: ice-on-valid-code
Depends on:
Blocks: 32834
  Show dependency treegraph
 
Reported: 2007-03-18 22:28 UTC by Thomas Koenig
Modified: 2007-08-30 22:13 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-06-11 12:54:18


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Koenig 2007-03-18 22:28:35 UTC
In trying to reduce PR 31197, I stumbled across a segfault:

$ cat bar.f90
CHARACTER(LEN=3), DIMENSION(10) :: Z
CHARACTER(LEN=10) :: res
Z(:)="123"
write(*,'(10A1)') TRANSPOSE(RESHAPE(Z(:)(2:2),(/5,2/)))
END
...
(gdb) r bar.f90
Starting program: /home/ig25/libexec/gcc/i686-pc-linux-gnu/4.3.0/f951 bar.f90
Failed to read a valid object file image from memory.
 MAIN__
Program received signal SIGSEGV, Segmentation fault.
0x080ccca3 in gfc_conv_function_call (se=0xbf8680e0, sym=0x880f9e8,
    arg=0x8849650, append_args=0x0)
    at /home/ig25/gcc/trunk/gcc/fortran/trans-expr.c:2032
2032      need_interface_mapping = ((sym->ts.type == BT_CHARACTER
(gdb) bt
#0  0x080ccca3 in gfc_conv_function_call (se=0xbf8680e0, sym=0x880f9e8,
    arg=0x8849650, append_args=0x0)
    at /home/ig25/gcc/trunk/gcc/fortran/trans-expr.c:2032
#1  0x080d7edb in gfc_conv_intrinsic_funcall (se=0xbf8680e0, expr=0x8849c78)
    at /home/ig25/gcc/trunk/gcc/fortran/trans-intrinsic.c:1514
#2  0x080cd407 in gfc_conv_function_expr (se=0xbf8680e0, expr=0x880f9e8)
    at /home/ig25/gcc/trunk/gcc/fortran/trans-expr.c:2657
#3  0x080cdb89 in gfc_conv_expr (se=0xbf8680e0, expr=0x8849c78)
    at /home/ig25/gcc/trunk/gcc/fortran/trans-expr.c:3097
#4  0x080b9ebd in gfc_add_loop_ss_code (loop=0xbf8681ec, ss=0x880f900,
    subscript=0 '\0') at /home/ig25/gcc/trunk/gcc/fortran/trans-array.c:1803
#5  0x080ba766 in gfc_conv_loop_setup (loop=0xbf8681ec)
    at /home/ig25/gcc/trunk/gcc/fortran/trans-array.c:3232
#6  0x080db6fc in gfc_trans_transfer (code=0x884a2f0)
    at /home/ig25/gcc/trunk/gcc/fortran/trans-io.c:1850
#7  0x080b17a9 in gfc_trans_code (code=0x884a2f0)
    at /home/ig25/gcc/trunk/gcc/fortran/trans.c:605
#8  0x080ddc04 in build_dt (function=0xb7bba310, code=0x884a3b0)
    at /home/ig25/gcc/trunk/gcc/fortran/trans-io.c:1507
#9  0x080b17cb in gfc_trans_code (code=0x884a3b0)
    at /home/ig25/gcc/trunk/gcc/fortran/trans.c:581
#10 0x080c75e3 in gfc_generate_function_code (ns=0x8848d50)
    at /home/ig25/gcc/trunk/gcc/fortran/trans-decl.c:3217

...
Comment 1 Francois-Xavier Coudert 2007-03-21 00:01:01 UTC
(In reply to comment #0)
> CHARACTER(LEN=3), DIMENSION(10) :: Z
> CHARACTER(LEN=10) :: res
> Z(:)="123"
> write(*,'(10A1)') TRANSPOSE(RESHAPE(Z(:)(2:2),(/5,2/)))
> END

Same thing happens with
  write(*,'(10A1)') RESHAPE(Z(:)(2:2),(/5,2/))

The segfault is due to the reshape symbol not having a charlen, and this is in turn due to the source argument of RESHAPE (that is Z(:)(2:)) not having a charlen itself: gfc_resolve_reshape simply copies the typespec of the source arg into the result.
Comment 2 Paul Thomas 2007-04-08 09:40:39 UTC
This fixes this PR and PR31197.  It needs some work because it still fails with variable expressions in the string length and I need to figure out which other intrinsics, if any, need the same treatment.

Paul

Index: /svn/trunk/gcc/fortran/iresolve.c
===================================================================
*** /svn/trunk/gcc/fortran/iresolve.c   (revision 123646)
--- /svn/trunk/gcc/fortran/iresolve.c   (working copy)
*************** gfc_resolve_reshape (gfc_expr *f, gfc_ex
*** 1722,1727 ****
--- 1722,1755 ----
        break;
      }

+   if (source->ts.type == BT_CHARACTER
+       && source->ref != NULL)
+     {
+       gfc_ref *ref;
+       for (ref = source->ref; ref; ref = ref->next)
+       if (ref->next == NULL && ref->type == REF_SUBSTRING)
+         break;
+       if (ref != NULL)
+       {
+         f->ts.type = BT_CHARACTER;
+         f->ts.kind = gfc_default_character_kind;
+         f->ts.cl = gfc_get_charlen ();
+         f->ts.cl->next = gfc_current_ns->cl_list;
+         gfc_current_ns->cl_list = f->ts.cl;
+         f->ts.cl->length = gfc_get_expr ();
+         f->ts.cl->length->expr_type = EXPR_OP;
+         f->ts.cl->length->value.op.operator = INTRINSIC_PLUS;
+         f->ts.cl->length->value.op.op1 = gfc_get_expr ();
+         f->ts.cl->length->value.op.op1->expr_type = EXPR_OP;
+         f->ts.cl->length->value.op.op1->value.op.operator = INTRINSIC_MINUS;
+         f->ts.cl->length->value.op.op1->value.op.op1 = gfc_copy_expr (ref->u.ss.end);
+         f->ts.cl->length->value.op.op1->value.op.op2 = gfc_copy_expr (ref->u.ss.start);
+
+         f->ts.cl->length->value.op.op2 =  gfc_int_expr (1);
+         gfc_simplify_expr (f->ts.cl->length, 0);
+       }
+     }
+
    /* TODO: Make this work with a constant ORDER parameter.  */
    if (shape->expr_type == EXPR_ARRAY
        && gfc_is_constant_expr (shape)
Comment 3 Thomas Koenig 2007-05-27 09:04:37 UTC
The problem seems to extend to pretty much all array intrinsics:

$ gfortran cshift.f90
cshift.f90: In function 'MAIN__':
cshift.f90:3: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.


$ cat eoshift.f90
CHARACTER(LEN=3), DIMENSION(10) :: Z
CHARACTER(LEN=10) :: res
Z(:)="123"
write(*,'(10A1)') EOSHIFT(Z(:)(2:2),2)
END
$ gfortran eoshift.f90
eoshift.f90: In function 'MAIN__':
eoshift.f90:3: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.


$ cat spread.f90
CHARACTER(LEN=3), DIMENSION(10) :: Z
CHARACTER(LEN=10) :: res
Z(:)="123"
write(*,'(10A1)') SPREAD(Z(:)(2:2),dim=1,ncopies=2)
END
$ gfortran spread.f90
spread.f90: In function 'MAIN__':
spread.f90:3: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.


$ cat transpose.f90
CHARACTER(LEN=3), DIMENSION(10,10) :: Z
CHARACTER(LEN=10) :: res
Z(:,:)="123"
write(*,'(10A1)') TRANSPOSE(Z(:,:)(2:2))
END
$ gfortran transpose.f90
transpose.f90: In function 'MAIN__':
transpose.f90:3: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.
Comment 4 Thomas Koenig 2007-05-27 09:06:17 UTC
I forgot the source code for the cshift case:

$ cat cshift.f90
CHARACTER(LEN=3), DIMENSION(10) :: Z
CHARACTER(LEN=10) :: res
Z(:)="123"
write(*,'(10A1)') CSHIFT(Z(:)(2:2),2)
END
$ gfortran cshift.f90
cshift.f90: In function 'MAIN__':
cshift.f90:3: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.
Comment 5 Thomas Koenig 2007-05-27 10:28:28 UTC
pack and unpack are also affected:

$ cat pack.f90
CHARACTER(LEN=3), DIMENSION(10,10) :: Z
CHARACTER(LEN=10) :: res
Z="123"
write(*,'(10A1)') pack(Z(:,:)(2:2),mask=.true.)
END
$ gfortran pack.f90
pack.f90: In function 'MAIN__':
pack.f90:3: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.

$ cat unpack.f90
CHARACTER(LEN=3), DIMENSION(10) :: Z
logical, dimension(5,2) :: mask
CHARACTER(LEN=10) :: res
Z(:)="123"
mask = .true.
write(*,'(10A1)') unpack(Z(:)(2:2),mask,' ')
END
$ gfortran unpack.f90
unpack.f90: In function 'MAIN__':
unpack.f90:5: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.
Comment 6 Paul Thomas 2007-06-11 12:54:18 UTC
A fix is coming for this tonight or tomorrow.

Paul
Comment 7 Paul Thomas 2007-08-30 22:11:13 UTC
Subject: Bug 31258

Author: pault
Date: Thu Aug 30 22:10:55 2007
New Revision: 127939

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=127939
Log:
2007-08-31  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/31879
	PR fortran/31197
	PR fortran/31258
	PR fortran/32703
	* gfortran.h : Add prototype for gfc_resolve_substring_charlen.
	* resolve.c (gfc_resolve_substring_charlen): New function.
	(resolve_ref): Call gfc_resolve_substring_charlen.
	(gfc_resolve_character_operator): New function.
	(gfc_resolve_expr): Call the new functions in cases where the
	character length is missing.
	* iresolve.c (cshift, eoshift, merge, pack, reshape, spread,
	transpose, unpack): Call gfc_resolve_substring_charlen for
	source expressions that are character and have a reference.
	* trans.h (gfc_trans_init_string_length) Change name to
	gfc_conv_string_length; modify references in trans-expr.c,
	trans-array.c and trans-decl.c.
	* trans-expr.c (gfc_trans_string_length): Handle case of no
	backend_decl.
	(gfc_conv_aliased_arg): Remove code for treating substrings
	and replace with call to gfc_trans_string_length.
	* trans-array.c (gfc_conv_expr_descriptor): Remove code for
	treating strings and call gfc_trans_string_length instead.

2007-08-31  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/31879
	* gfortran.dg/char_length_7.f90: New test.
	* gfortran.dg/char_length_9.f90: New test.
	* gfortran.dg/char_assign_1.f90: Add extra warning.

	PR fortran/31197
	PR fortran/31258
	* gfortran.dg/char_length_8.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/char_length_7.f90
    trunk/gcc/testsuite/gfortran.dg/char_length_8.f90
    trunk/gcc/testsuite/gfortran.dg/char_length_9.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/iresolve.c
    trunk/gcc/fortran/resolve.c
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans.h
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/char_assign_1.f90

Comment 8 Paul Thomas 2007-08-30 22:13:54 UTC
Fixed on trunk.

Thanks for the report!

Paul