From: Steven Smith <sos22@cam.ac.uk>
To: Shanks <mshanks79@yahoo.co.in>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: strange gcc warning
Date: Mon, 13 Jan 2003 18:26:13 +0000 [thread overview]
Message-ID: <20030113182613.GA11191@cam.ac.uk> (raw)
In-Reply-To: <20030113175755.71066.qmail@web8001.mail.in.yahoo.com>
[-- Attachment #1: Type: text/plain, Size: 1091 bytes --]
> void
> my_print(int errno)
Defining a parameter to shadow a global variable is always a bad idea,
but it's an especially bad idea for errno. In multi-threaded programs,
and programs compiled with a certain set of flags, errno is actually a
preprocessor macro. Usually, when using glibc, errno expands to
(*__errno_location()), and so your prototype expands to void
my_print(int (*__errno_location())). gcc interprets this as a pointer
to a function returning an int pointer, which was probably not what
you expected.
> {
> printf("ERRNO: %d\n", errno);
Now, this expands to ``printf("ERRNO: %d\n", (*__errno_location()));".
__errno_location is a function pointer returning an int *, and so
this calls the first argument, and then dereferences the returned
value.
> }
>
> int
> main()
> {
> int a = 10;
> errno = 0;
> my_print(a);
a is now implicitly converted to a pointer to function returning int
*. When the code is run, my_print tries to call a function at address
10 in memory, and crashes.
> }
Steven Smith,
sos22@cam.ac.uk.
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
next prev parent reply other threads:[~2003-01-13 18:26 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-01-13 17:57 strange gcc warning Shanks
2003-01-13 18:26 ` Steven Smith [this message]
2003-01-14 6:00 ` Shanks
2003-01-13 19:25 ` Elias Athanasopoulos
2003-01-13 18:07 ` Shanks
2003-01-13 21:35 ` Marius Nita
-- strict thread matches above, loose matches on Subject: below --
2003-01-14 7:01 Govind Raghuram
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=20030113182613.GA11191@cam.ac.uk \
--to=sos22@cam.ac.uk \
--cc=linux-c-programming@vger.kernel.org \
--cc=mshanks79@yahoo.co.in \
/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 NNTP newsgroup(s).