public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: chitty@libero.it
To: gcc-gnats@gcc.gnu.org
Cc: alessandro_amici@telespazio.it
Subject: c/4101: gcc 2.95.4 compiles bad code with nested static functions
Date: Thu, 23 Aug 2001 11:26:00 -0000	[thread overview]
Message-ID: <20010823182552.30240.qmail@sourceware.cygnus.com> (raw)

>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:


             reply	other threads:[~2001-08-23 11:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-23 11:26 chitty [this message]
2001-08-23 12:16 Bernd Schmidt
2001-08-23 12:42 neil

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010823182552.30240.qmail@sourceware.cygnus.com \
    --to=chitty@libero.it \
    --cc=alessandro_amici@telespazio.it \
    --cc=gcc-gnats@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).