From: "sarah@hederstierna.com" <fredrik@hederstierna.com>
To: "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Warn if making external references to local stack memory?
Date: Mon, 09 Apr 2012 05:55:00 -0000 [thread overview]
Message-ID: <CE36BD26828FA5408B9F87E4DD2ACB0B016A8AF5E977@MBXVS01.HMC.local> (raw)
Hi
GCC does warn if returning a pointer to a local variable (stack memory).
But there are alot of more cases where GCC could possibly warn,
eg. when references are made to local variables or stack memory.
See this attached example code.
GCC warns for first case, but not the others.
I think all cases can be considered program bugs,
and could trigger a compiler warning I think.
I've found out that the present warning is done in "c-typeck.c",
is this the right place to but additional warnings of this kind too?
Thanks & Best Regards
Fredrik Hederstierna
The example code file
Compiled with "-O2 -W -Wall -Wextra"
---------------------------
#include <stdio.h>
#include <stdlib.h>
int * test_ptr;
struct test {
int *ptr;
};
int* test_return_ptr_to_stack_mem(void)
{
int a[100];
// CORRECT WARNING:
// "warning: function returns address of local variable".
// (Checking done in file gcc/c-typeck.c, function c_finish_return()).
return a;
}
void test_set_ptr_to_stack_mem(void)
{
int a[100];
// GIVE WARNING?
// "function returns with external reference to local variable?"
test_ptr = a;
return;
}
void* test_alloc_struct_ptr_to_stack_mem(void)
{
int a[100];
struct test* t = (struct test*)malloc(sizeof(struct test));
// GIVE WARNING?
// "function returns with reference to local variable?"
t->ptr = a;
return t;
}
void* test_alloc_struct_on_stack_mem(void)
{
struct test* t = (struct test*)alloca(sizeof(struct test));
t->ptr = NULL;
// GIVE WARNING?
// "function returns allocation from stack memory?"
return t;
}
int main(void)
{
// GIVES WARNING
int* t1 = test_return_ptr_to_stack_mem();
printf("Stack mem ref test 1: %p\n", t1);
// NO WARNING?
test_set_ptr_to_stack_mem();
printf("Stack mem ref test 2: %d\n", test_ptr[0]);
// NO WARNING?
struct test * t3 = test_alloc_struct_ptr_to_stack_mem();
printf("Stack mem ref test 3: %d\n", t3->ptr[0]);
// NO WARNING?
struct test * t4 = test_alloc_struct_on_stack_mem();
printf("Stack mem ref test 4: %p\n", t4->ptr);
return 0;
}
next reply other threads:[~2012-04-09 5:55 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-09 5:55 sarah@hederstierna.com [this message]
2012-04-10 4:43 ` Ian Lance Taylor
2012-04-10 7:49 Fredrik Hederstierna
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=CE36BD26828FA5408B9F87E4DD2ACB0B016A8AF5E977@MBXVS01.HMC.local \
--to=fredrik@hederstierna.com \
--cc=gcc@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).