public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: c/4101: gcc 2.95.4 compiles bad code with nested static functions
@ 2001-08-23 12:42 neil
  0 siblings, 0 replies; 3+ messages in thread
From: neil @ 2001-08-23 12:42 UTC (permalink / raw)
  To: alessandro_amici, chitty, gcc-bugs, gcc-prs, nobody

Synopsis: gcc 2.95.4 compiles bad code with nested static functions

State-Changed-From-To: open->closed
State-Changed-By: neil
State-Changed-When: Thu Aug 23 12:42:01 2001
State-Changed-Why:
    Duplicate of 4103.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=4101&database=gcc


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: c/4101: gcc 2.95.4 compiles bad code with nested static functions
@ 2001-08-23 12:16 Bernd Schmidt
  0 siblings, 0 replies; 3+ messages in thread
From: Bernd Schmidt @ 2001-08-23 12:16 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c/4101; it has been noted by GNATS.

From: Bernd Schmidt <bernds@redhat.com>
To: <chitty@libero.it>
Cc: <gcc-gnats@gcc.gnu.org>, <alessandro_amici@telespazio.it>,
   <nobody@gcc.gnu.org>, <gcc-prs@gcc.gnu.org>, <gcc-bugs@gcc.gnu.org>
Subject: Re: c/4101: gcc 2.95.4 compiles bad code with nested static functions
Date: Thu, 23 Aug 2001 20:15:16 +0100 (BST)

 (I currently can't access the gnatsweb page for some reason, otherwise
 I'd have closed this.  If gnats works for someone, please close...).
 
 > /* this must do a certain thing only the 1st time,
 >    but we don't want a check as if (first_time) {do},
 >    so we use a function pointer that the first time
 >    does, the others just returns.
 >  */
 > void loop(void)
 > {
 >    void f1(void);
 >    void f2(void);
 >    static void (*f)(void) = f1;
 >
 >    void f2(void) { } /* this is empty */
 >
 >    void f1(void)
 >    {
 >       /* ... do something */
 >       f=f2; /* the problem is HERE */
 >    }
 >
 >    (*f)(); /* do something only the first time */
 > }
 
 This isn't actually valid.  The line
 
 >    static void (*f)(void) = f1;
 
 constructs a trampoline on the current stack of the function loop, and
 once you've exited from that function, the contents of the stack are
 no longer valid.  Basically, f1 is treated as a local variable, and you
 can't keep pointers to those across multiple calls to a function either.
 
 
 Bernd
 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* c/4101: gcc 2.95.4 compiles bad code with nested static functions
@ 2001-08-23 11:26 chitty
  0 siblings, 0 replies; 3+ messages in thread
From: chitty @ 2001-08-23 11:26 UTC (permalink / raw)
  To: gcc-gnats; +Cc: alessandro_amici

>Number:         4101
>Category:       c
>Synopsis:       gcc 2.95.4 compiles bad code with nested static functions
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Thu Aug 23 11:26:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Cristiano Milia
>Release:        gcc version 2.95.4 20010319 (Debian prerelease)
>Organization:
>Environment:
Linux 2.4.6-mosix #1 SMP i686 
>Description:
f2 is a function defined into another function body, and f pointer is declared static in the same scope of f2.

when assigning address of f2 to f, the correct assignment is correctly done ONLY IF the BODY of f2 is defined AFTER the assignment statement.

the result of this wrong address generation is an "illegal instruction" at runtime, when executing (*f)().

the fact is that f is assigned a value that is NOT the actual f2 value.
it seems to me that if f2 is DEFINED before the assignment statement, gcc gets confused about its address, that is not the one f2 had when it was DECLARED. 

function_2 can be have an empty body or not, it always happens.
>How-To-Repeat:
/* this must do a certain thing only the 1st time,
   but we don't want a check as if (first_time) {do},
   so we use a function pointer that the first time
   does, the others just returns.
 */
void loop(void) 
{
   void f1(void);
   void f2(void);
   static void (*f)(void) = f1;

   void f2(void) { } /* this is empty */

   void f1(void)
   {
      /* ... do something */
      f=f2; /* the problem is HERE */
   }

   (*f)(); /* do something only the first time */
}

int main()
{
   loop();
   loop(); /* second time it crashes! */
}
>Fix:
just put the DEFINITION of f2 AFTER the assignment statement:

void loop(void)
{
   void f1(void);
   void f2(void);

   static void (*f)(void) = f1;

   void f1(void)
   {
      f=f2; /* NOW it correctly assigns the address of f2 */
   }

   void f2(void) { }

   (*f)();
}
>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-08-23 12:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-23 12:42 c/4101: gcc 2.95.4 compiles bad code with nested static functions neil
  -- strict thread matches above, loose matches on Subject: below --
2001-08-23 12:16 Bernd Schmidt
2001-08-23 11:26 chitty

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).