Bug 29396 - segfault with character pointer association
Summary: segfault with character pointer association
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.2.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords: ice-on-valid-code
: 31212 31220 (view as bug list)
Depends on:
Blocks: 19276 32834
  Show dependency treegraph
 
Reported: 2006-10-08 21:45 UTC by Francois-Xavier Coudert
Modified: 2007-09-16 09:40 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.2.0 4.3.0
Last reconfirmed: 2007-09-03 11:46:39


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Francois-Xavier Coudert 2006-10-08 21:45:57 UTC
$ cat a3.f90
  CHARACTER(LEN=2), DIMENSION(:), POINTER :: a 
  CHARACTER(LEN=4), DIMENSION(3), TARGET :: b 
  b=(/"bbbb","bbbb","bbbb"/) 
  a=>b(:)(2:3) 
  a="aa" 
  IF (ANY(b.NE.(/"baab","baab","baab"/))) CALL ABORT() 
  END 
$ gfortran a3.f90 
a3.f90:0: internal compiler error: Segmentation fault


gdb backtrace is:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004179b2 in gfc_check_pointer_assign (lvalue=0xce3ae0, 
    rvalue=0xce3de0) at ../../trunk/gcc/fortran/expr.c:2330
2330      if (lvalue->ts.type == BT_CHARACTER
(gdb) where
#0  0x00000000004179b2 in gfc_check_pointer_assign (lvalue=0xce3ae0, 
    rvalue=0xce3de0) at ../../trunk/gcc/fortran/expr.c:2330
#1  0x000000000044a349 in resolve_code (code=0xce42c0, ns=0xce2380)
    at ../../trunk/gcc/fortran/resolve.c:4967
#2  0x000000000044b765 in gfc_resolve (ns=0xce2380)
    at ../../trunk/gcc/fortran/resolve.c:6919
#3  0x0000000000440a68 in gfc_parse_file ()
    at ../../trunk/gcc/fortran/parse.c:3212

2330      if (lvalue->ts.type == BT_CHARACTER
2331            && lvalue->ts.cl->length && rvalue->ts.cl->length
2332            && abs (gfc_dep_compare_expr (lvalue->ts.cl->length,
2333                                          rvalue->ts.cl->length)) == 1)

(gdb) p lvalue->ts
$1 = {type = BT_CHARACTER, kind = 1, derived = 0x0, cl = 0xcb0800}
(gdb) p rvalue->ts
$2 = {type = BT_CHARACTER, kind = 1, derived = 0x0, cl = 0x0}

So rvalue->ts.cl->length should not be referenced.
Comment 1 Jerry DeLisle 2007-01-28 22:15:31 UTC
In the spirit of Paul's suggestion.  I will try this one.
Comment 2 Jerry DeLisle 2007-01-30 23:24:07 UTC
Un-assigning myself.  Don't have time to delve into the deeper problem here which may require changes to array descriptors. After brief exchange with Paul off list.

Comment 3 Francois-Xavier Coudert 2007-03-16 15:13:50 UTC
*** Bug 31220 has been marked as a duplicate of this bug. ***
Comment 4 Francois-Xavier Coudert 2007-03-16 15:15:54 UTC
Joost filed PR31220, which is a duplicate of this one, but contains an interesting testcase (compile & run) to check and add to the testsuite when we're done with this bug:

CHARACTER(LEN=4), POINTER :: b
CHARACTER(LEN=10) :: r
b=>F1(1,r)
b="1111"
b=>F1(2,r)
b="2222"
b=>F1(3,r)
b="3333"
IF (r.NE."12222-----") CALL ABORT()
CONTAINS
FUNCTION F1(I,r) result(b)
INTEGER :: I
CHARACTER(LEN=10), TARGET, SAVE :: a="----------"
CHARACTER(LEN=10) :: r
CHARACTER(LEN=4), POINTER :: b
r=a
b=>a(I:I+3)
END FUNCTION
END
Comment 5 Francois-Xavier Coudert 2007-03-16 19:30:04 UTC
*** Bug 31212 has been marked as a duplicate of this bug. ***
Comment 6 Jerry DeLisle 2007-05-05 23:54:56 UTC
The test case in comment #4 appears to be working fine on my system here.  I suspect it was not a duplicate after all.
Comment 7 patchapp@dberlin.org 2007-09-10 02:42:22 UTC
Subject: Bug number PR29396

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-09/msg00779.html
Comment 8 Paul Thomas 2007-09-16 09:18:19 UTC
Subject: Bug 29396

Author: pault
Date: Sun Sep 16 09:17:49 2007
New Revision: 128523

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

	PR fortran/29396
	PR fortran/29606
	PR fortran/30625
	PR fortran/30871
	* trans.h : Add extra argument to gfc_build_array_ref. Rename
	gfc_conv_aliased_arg to gfc_conv_subref_array_arg.  Move
	prototype of is_aliased_array to gfortran.h and rename it
	gfc_is_subref_array.  Add field span to lang_decl, add a new
	decl lang specific flag accessed by GFC_DECL_SUBREF_ARRAY_P
	and a new type flag GFC_DECL_SUBREF_ARRAY_P.
	* trans.c (gfc_build_array_ref): Add the new argument, decl.
	If this is a subreference array pointer, use the lang_decl
	field 'span' to calculate the offset in bytes and use pointer
	arithmetic to address the element.
	* trans-array.c (gfc_conv_scalarized_array_ref,
	gfc_conv_array_ref): Add the backend declaration as the third
	field, if it is likely to be a subreference array pointer.
	(gfc_conv_descriptor_dimension, gfc_trans_array_ctor_element,
	gfc_trans_array_constructor_element, structure_alloc_comps,
	gfc_conv_array_index_offset): For all other references to
	gfc_build_array_ref, set the third argument to NULL.
	(gfc_get_dataptr_offset): New function.
	(gfc_conv_expr_descriptor): If the rhs of a pointer assignment
	is a subreference array, then calculate the offset to the
	subreference of the first element and set the descriptor data
	pointer to this, using gfc_get_dataptr_offset.
	trans-expr.c (gfc_get_expr_charlen): Use the expression for the
	character length for a character subreference.
	(gfc_conv_substring, gfc_conv_subref_array_arg): Add NULL for
	third argument in call to gfc_build_array_ref.
	(gfc_conv_aliased_arg): Rename to gfc_conv_subref_array_arg.
	(is_aliased_array): Remove.
	(gfc_conv_function_call): Change reference to is_aliased_array
	to gfc_is_subref_array and reference to gfc_conv_aliased_arg to
	gfc_conv_subref_array_arg.
	(gfc_trans_pointer_assignment): Add the array element length to
	the lang_decl 'span' field.
	* gfortran.h : Add subref_array_pointer to symbol_attribute and
	add the prototype for gfc_is_subref_array.
	* trans-stmt.c : Add NULL for third argument in all references
	to gfc_build_array_ref.
	* expr.c (gfc_is_subref_array): Renamed is_aliased_array.
	If this is a subreference array pointer, return true.
	(gfc_check_pointer_assign): If the rhs is a subreference array,
	set the lhs subreference_array_pointer attribute.
	* trans-decl.c (gfc_get_symbol_decl): Allocate the lang_decl
	field if the symbol is a subreference array pointer and set an
	initial value of zero for the 'span' field.
	* trans-io.c (set_internal_unit): Refer to is_subref_array and
	gfc_conv_subref_array_arg.
	(nml_get_addr_expr): Add NULL third argument to
	gfc_build_array_ref. 
	(gfc_trans_transfer): Use the scalarizer for a subreference
	array.

2007-09-16  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/29396
	PR fortran/29606
	PR fortran/30625
	PR fortran/30871
	* gfortran.dg/subref_array_pointer_1.f90: New test.
	* gfortran.dg/subref_array_pointer_2.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/subref_array_pointer_1.f90
    trunk/gcc/testsuite/gfortran.dg/subref_array_pointer_2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans-io.c
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/fortran/trans.c
    trunk/gcc/fortran/trans.h
    trunk/gcc/testsuite/ChangeLog

Comment 9 Paul Thomas 2007-09-16 09:40:48 UTC
Fixed on trunk

Paul