public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Warn if making external references to local stack memory?
@ 2012-04-10  7:49 Fredrik Hederstierna
  0 siblings, 0 replies; 3+ messages in thread
From: Fredrik Hederstierna @ 2012-04-10  7:49 UTC (permalink / raw)
  To: gcc; +Cc: iant


>> 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.
>
> Thanks.  I would encourage you to file an enhancement request at
> http://gcc.gnu.org/bugzilla/ .
>
>> 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?
>
> Since these sorts of errors can occur in both C and C++ it would be
> slightly better to use a common framework for them if possible, perhaps
> in c-family/c-common.c.  I haven't really thought about what is required
> here, though.

I've added the following enhancement request to bugzilla

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52923
"Bug 52923 - Warn if making external references to local stack memory"

Thanks & Best Regards
Fredrik

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

* Re: Warn if making external references to local stack memory?
  2012-04-09  5:55 sarah@hederstierna.com
@ 2012-04-10  4:43 ` Ian Lance Taylor
  0 siblings, 0 replies; 3+ messages in thread
From: Ian Lance Taylor @ 2012-04-10  4:43 UTC (permalink / raw)
  To: sarah@hederstierna.com; +Cc: gcc

"sarah@hederstierna.com" <fredrik@hederstierna.com> writes:

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

Thanks.  I would encourage you to file an enhancement request at
http://gcc.gnu.org/bugzilla/ .


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

Since these sorts of errors can occur in both C and C++ it would be
slightly better to use a common framework for them if possible, perhaps
in c-family/c-common.c.  I haven't really thought about what is required
here, though.

Ian

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

* Warn if making external references to local stack memory?
@ 2012-04-09  5:55 sarah@hederstierna.com
  2012-04-10  4:43 ` Ian Lance Taylor
  0 siblings, 1 reply; 3+ messages in thread
From: sarah@hederstierna.com @ 2012-04-09  5:55 UTC (permalink / raw)
  To: gcc

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;
}

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

end of thread, other threads:[~2012-04-10  7:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-10  7:49 Warn if making external references to local stack memory? Fredrik Hederstierna
  -- strict thread matches above, loose matches on Subject: below --
2012-04-09  5:55 sarah@hederstierna.com
2012-04-10  4:43 ` Ian Lance Taylor

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).