From nobody@FreeBSD.ORG Mon Jun 28 07:57:16 1999
Return-Path: <nobody@FreeBSD.ORG>
Received: by hub.freebsd.org (Postfix, from userid 32767)
	id 99A6B1515E; Mon, 28 Jun 1999 07:57:16 -0700 (PDT)
Message-Id: <19990628145716.99A6B1515E@hub.freebsd.org>
Date: Mon, 28 Jun 1999 07:57:16 -0700 (PDT)
From: dima@server.ru
Sender: nobody@FreeBSD.ORG
To: freebsd-gnats-submit@freebsd.org
Subject: f2c works incorrectly with arguments of subroutine with multiple entry points
X-Send-Pr-Version: www-1.0

>Number:         12431
>Category:       bin
>Synopsis:       f2c works incorrectly with arguments of subroutine with multiple entry points
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Jun 28 08:00:00 PDT 1999
>Closed-Date:    Sun Aug 29 13:16:35 PDT 1999
>Last-Modified:  Sun Aug 29 13:17:36 PDT 1999
>Originator:     Dmitry Kazarov
>Release:        
>Organization:
Server Inc
>Environment:
>Description:
The arguments of subroutine with multiple entry points could be used as any other variables anywhere withing body of subroutine.
So argument of one entry point (or it's value) should be copied to local static variable if it used in the call to other entry point.

This is valid FORTRAN code (works with many FORTRAN compilers but f2c):
	subroutine a(x, y, z)
	z=x+y
	return
	entry b(i)
	i=z
	return
	end
f2c will produce this C code:
/* Subroutine */ int a_0_(n__, x, y, z__, i__)
int n__;
real *x, *y, *z__;
integer *i__;
{
    switch(n__) {
	case 1: goto L_b;
	}

    *z__ = *x + *y;
    return 0;

L_b:
    *i__ = *z__;
    return 0;
} /* a_ */

/* Subroutine */ int a_(x, y, z__)
real *x, *y, *z__;
{
    return a_0_(0, x, y, z__, (integer *)0);
    }

/* Subroutine */ int b_(i__)
integer *i__;
{
    return a_0_(1, (real *)0, (real *)0, (real *)0, i__);
    }

On the call to b argument z points to 0, what cause core dump.
>How-To-Repeat:

>Fix:
Before exit from functions generated for entry points values of arguments should be copied to static variables. In calls to common function all pointers to 0 should be changed to pointers to that static variables.

>Release-Note:
>Audit-Trail:

From: "Steven G. Kargl" <kargl@apl.washington.edu>
To: freebsd-gnats-submit@freebsd.org, dima@server.ru
Cc:  
Subject: Re: bin/12431: f2c works incorrectly with arguments of subroutine with 
 multiple entry points
Date: Fri, 13 Aug 1999 15:03:10 -0700

 This PR should be closed.   f2c is no longer supported with the
 base distribution of FreeBSD-current.
 
 -- 
 Steve
 
State-Changed-From-To: open->closed 
State-Changed-By: mph 
State-Changed-When: Fri Aug 13 18:54:07 EDT 1999 
State-Changed-Why:  
As pointed out by Steven G. Kargl <kargl@apl.washington.edu>, FreeBSD 
now uses egcs's f77 as its Fortran compiler instead of f2c.  Thank you 
for your report. 
State-Changed-From-To: closed->open 
State-Changed-By: mph 
State-Changed-When: Mon Aug 16 10:08:11 EDT 1999 
State-Changed-Why:  
Submitter is correct.  The problem still exists on 4.0-CURRENT with 
out current f77.  Sorry for prematurely closing this PR. 
(s/out/our/) 

From: "Steven G. Kargl" <kargl@apl.washington.edu>
To: freebsd-gnats-submit@freebsd.org, dima@server.ru
Cc:  
Subject: Re: bin/12431: f2c works incorrectly with arguments of subroutine with 
 multiple entry points
Date: Mon, 16 Aug 1999 09:53:18 -0700

 The PR should be closed.  Upon closer inspection it appears that the 
 Dima's codes relies on undefine behavour.
 
       subroutine a(x,y,z)
       z=x+y
       return
       entry b(i)
       i=z
       return
       end
 
       program main
       call a(1.,2.,z)
       write(*,*) z
       call b(i)
       write(*,*) i
       stop
       end
 
 Z is a dummy argument to subroutine a().  Z goes out of scope
 when a() returns to main.  When b() is called, z is an undefined
 variable.
 
 The correct fix is:
 
       subroutine a(x,y,z)
       real zz
       save zz
       data zz /0./
       z=x+y
       zz=z
       return
       entry b(i)
       i=zz
       return
       end
 
 because the SAVE attribute can be used on local variables while
 it can not be used with dummy variables.
 
 -- 
 Steve
 
State-Changed-From-To: open->closed 
State-Changed-By: steve 
State-Changed-When: Sun Aug 29 13:16:35 PDT 1999 
State-Changed-Why:  
I didn't hear any arguments to Steven Kargl claim that this program relied 
on undefined behavior. 
>Unformatted:
