Bug 16939 - Pointers not passed as subroutine arguments
Summary: Pointers not passed as subroutine arguments
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: 4.0.1
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on: 17192 17193
Blocks: Fortran_character
  Show dependency treegraph
 
Reported: 2004-08-09 14:27 UTC by Paul Thomas
Modified: 2005-06-05 22:55 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-04-06 18:35:35


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Thomas 2004-08-09 14:27:25 UTC
! This compiles OK but gives gibberish output or segmentation faults.  It seems
! that pointers are not being transferred correctly between sub-programs

!  copyright 1996 Loren P. Meissner -- May be distributed if this line is included. 
!  Earlier versions of ELF failed because INTENT was not declared for pointers. 
 
  program Pointer_Arg_Intent 
    implicit none 
    integer, parameter :: Str_Len = 15 
    character (len = Str_Len), pointer :: Root
    nullify (Root) 
    call Create (Root) 
!    print *, Root                        !causes segmentation fault at runtime
    print *, Get_Target (Root)            !outputs gibberish
    stop 
 
  contains 
 
    subroutine Create (Temp_Ptr) 
      character (len = Str_Len), pointer :: Temp_Ptr
      character (len = Str_Len), pointer :: New_Item
      allocate (New_Item) 
      New_Item = " A NEW STRING " 
      Temp_Ptr => New_Item
      print *, Temp_ptr                   !works OK
      return 
    end subroutine Create 
 
    function Get_Target (Temp_Ptr) result (String) 
      character (len = Str_Len), pointer :: Temp_Ptr 
      character (len = Str_Len) :: String 
      String = Temp_Ptr
      return 
    end function Get_Target 
 
  end program Pointer_Arg_Intent
Comment 1 Andrew Pinski 2004-08-14 07:53:11 UTC
Confirmed.
And here is the problem:
Note no & on the variable:
(int1[1 .. 15] * &)new_item.3

Comment 2 Andrew Pinski 2004-08-14 07:55:52 UTC
Well I was wrong on say note no & on the variable, really it should be like:
  *temp_ptr = new_item.3;
instead of what it is now:
  temp_ptr = (int1[1 .. 15] * &)new_item.3;

because otherwise temp_ptr gets assigned in create and does not get passed back.
Comment 3 Andrew Pinski 2005-04-06 18:37:40 UTC
This looks to me (a non fortran person) a duplicate of bug 17192.
Comment 4 Paul Thomas 2005-04-06 19:26:41 UTC
Subject: Re:  Pointers not passed as subroutine arguments


----- Original Message ----- 
From: "pinskia at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org>
To: <paulthomas2@wanadoo.fr>
Sent: Wednesday, April 06, 2005 8:37 PM
Subject: [Bug fortran/16939] Pointers not passed as subroutine arguments


>
> ------- Additional Comments From pinskia at gcc dot gnu dot org 
> 2005-04-06 18:37 -------
> This looks to me (a non fortran person) a duplicate of bug 17192.
>
> -- 
>           What    |Removed                     |Added
> ----------------------------------------------------------------------------
>  BugsThisDependsOn|                            |17192
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16939
>
> ------- You are receiving this mail because: -------
> You reported the bug, or are watching the reporter.
> 


Comment 5 Paul Thomas 2005-04-06 19:31:51 UTC
Subject: Re:  Pointers not passed as subroutine arguments

I think that it is,..... in principle.  The examples are a bit strange 
though.

Paul

----- Original Message ----- 
From: "pinskia at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org>
To: <paulthomas2@wanadoo.fr>
Sent: Wednesday, April 06, 2005 8:37 PM
Subject: [Bug fortran/16939] Pointers not passed as subroutine arguments


>
> ------- Additional Comments From pinskia at gcc dot gnu dot org 
> 2005-04-06 18:37 -------
> This looks to me (a non fortran person) a duplicate of bug 17192.
>
> -- 
>           What    |Removed                     |Added
> ----------------------------------------------------------------------------
>  BugsThisDependsOn|                            |17192
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16939
>
> ------- You are receiving this mail because: -------
> You reported the bug, or are watching the reporter.
> 


Comment 6 Tobias Schlüter 2005-05-09 13:40:28 UTC
What is wrong is this from the .t02.original dump:
create (temp_ptr, _temp_ptr)
{
  char[1:15] * new_item;

  {
    void * * ptr.0;

    ptr.0 = (void * *) &new_item;
    _gfortran_allocate (ptr.0, 15, 0);
  }
  _gfortran_copy_string (15, new_item, 14, " A NEW STRING ");
  temp_ptr = (char[1:15] * &) new_item;
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  _gfortran_filename = "pr16939.f90";
  _gfortran_line = 19;
  _gfortran_ioparm.unit = 6;
  _gfortran_ioparm.list_format = 1;
  _gfortran_st_write ();
  _gfortran_transfer_character (temp_ptr, 15);
  _gfortran_st_write_done ();
  goto __return_create;
  __return_create:;
}

The underlined line should be 
   temp_ptr = new_item
unless I misread something.  temp_ptr should point to the same character string
new_item points to.  The underlined statement makes it point to new_item instead.
Comment 7 Paul Thomas 2005-05-09 16:06:35 UTC
That's right Tobi

The patch that fixes this bug and regtests is:

Index: trans-expr.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/fortran/trans-expr.c,v
retrieving revision 1.42
diff -p -c -3 -r1.42 trans-expr.c
*** trans-expr.c        29 Apr 2005 15:31:33 -0000      1.42
--- trans-expr.c        9 May 2005 14:41:23 -0000
*************** gfc_conv_variable (gfc_se * se, gfc_expr
*** 360,365 ****
--- 360,372 ----
        if (sym->attr.dummy
          && sym->ts.type != BT_CHARACTER
          && !sym->attr.dimension)
+       se->expr = gfc_build_indirect_ref (se->expr);
+
+       /*Dereference dummy args that are character pointers. */
+       if (sym->attr.dummy
+         &&  sym->attr.pointer
+         && sym->ts.type == BT_CHARACTER
+         && !sym->attr.dimension)
        se->expr = gfc_build_indirect_ref (se->expr);

        /* Dereference pointer variables.  */

I will do an update, bootstrap and regtest tonight before submitting this to 
the lists.  It should be posted tonight.
Comment 8 Paul Thomas 2005-05-09 16:08:30 UTC
Subject: Re:  Pointers not passed as subroutine arguments

The following fixes PR16939 and regtests.  I'll do a full synchronisatio, 
bootstrap and regtest before posting it on the lists:

Best regards

Paul

Index: trans-expr.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/fortran/trans-expr.c,v
retrieving revision 1.42
diff -p -c -3 -r1.42 trans-expr.c
*** trans-expr.c        29 Apr 2005 15:31:33 -0000      1.42
--- trans-expr.c        9 May 2005 14:41:23 -0000
*************** gfc_conv_variable (gfc_se * se, gfc_expr
*** 360,365 ****
--- 360,372 ----
        if (sym->attr.dummy
          && sym->ts.type != BT_CHARACTER
          && !sym->attr.dimension)
+       se->expr = gfc_build_indirect_ref (se->expr);
+
+       /*Dereference dummy args that are character pointers. */
+       if (sym->attr.dummy
+         &&  sym->attr.pointer
+         && sym->ts.type == BT_CHARACTER
+         && !sym->attr.dimension)
        se->expr = gfc_build_indirect_ref (se->expr);

        /* Dereference pointer variables.  */


Comment 9 GCC Commits 2005-05-29 16:02:32 UTC
Subject: Bug 16939

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	pault@gcc.gnu.org	2005-05-29 16:02:11

Modified files:
	gcc/fortran    : trans-array.c trans-expr.c 

Log message:
	2005-05-29 Paul Thomas  <pault@gcc.gnu.org>
	
	PR fortran/16939
	PR fortran/17192
	PR fortran/17193
	PR fortran/17202
	PR fortran/18689
	PR fortran/18890
	PR fortran/21297
	* fortran/trans-array.c (gfc_conv_resolve_dependencies): Add string
	length to temp_ss for character pointer array assignments.
	* fortran/trans-expr.c (gfc_conv_variable): Correct errors in
	dereferencing of characters and character pointers.
	* fortran/trans-expr.c (gfc_conv_function_call): Provide string
	length as return argument for various kinds of handling of return.
	Return a char[]* temporary for character pointer functions and
	dereference the temporary upon return.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/trans-array.c.diff?cvsroot=gcc&r1=1.45&r2=1.46
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/trans-expr.c.diff?cvsroot=gcc&r1=1.44&r2=1.45

Comment 10 GCC Commits 2005-06-05 08:59:35 UTC
Subject: Bug 16939

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-4_0-branch
Changes by:	pault@gcc.gnu.org	2005-06-05 08:59:18

Modified files:
	gcc/fortran    : ChangeLog trans-array.c trans-expr.c 

Log message:
	005-05-31  Paul Thomas  <pault@gcc.gnu.org>
	
	PR fortran/18109
	PR fortran/18283
	PR fortran/19107
	PR fortran/16939
	PR fortran/17192
	PR fortran/17193
	PR fortran/17202
	PR fortran/18689
	PR fortran/18890
	* fortran/trans-array.c (gfc_conv_expr_descriptor): Obtain the
	string length from the expression typespec character length value
	and set temp_ss->stringlength and backend_decl. Obtain the
	tree expression from gfc_conv_expr rather than gfc_conv_expr_val.
	Dereference the expression to obtain the character.
	* fortran/trans-array.c (gfc_conv_resolve_dependencies): Add string
	length to temp_ss for character pointer array assignments.
	* fortran/trans-expr.c (gfc_conv_component_ref): Remove the
	dereference of scalar character pointer structure components.
	* fortran/trans-expr.c (gfc_trans_subarray_assign): Obtain the
	string length for the structure component from the component
	expression.
	* fortran/trans-expr.c (gfc_conv_variable): Correct errors in
	dereferencing of characters and character pointers.
	* fortran/trans-expr.c (gfc_conv_function_call): Provide string
	length as return argument for various kinds of handling of return.
	Return a char[]* temporary for character pointer functions and
	dereference the temporary upon return.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.335.2.66&r2=1.335.2.67
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/trans-array.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.39.2.3&r2=1.39.2.4
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/trans-expr.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.38.2.4&r2=1.38.2.5

Comment 11 Paul Thomas 2005-06-05 22:42:03 UTC
Fixed in 4.0 and 4.1