Bug 14156 - no warning for address of parameter
Summary: no warning for address of parameter
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.2
: P2 normal
Target Milestone: 4.0.0
Assignee: Andrew Pinski
URL:
Keywords: diagnostic, patch
: 14519 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-02-16 09:56 UTC by d.binderman
Modified: 2004-03-11 05:45 UTC (History)
3 users (show)

See Also:
Host: linux / x86
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-02-16 12:17:11


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description d.binderman 2004-02-16 09:56:24 UTC
// Given the following C++ source code

int * f( int a)
{
	return &a;
}

int * g()
{
	int b = 0;

	return &b;
}

/*
then

[dcb@localhost src]$ ~/gnu/gcc333pre2/results/bin/g++ -c -g -O2 -Wall -ansi
-pedantic addrLocal.cc
addrLocal.cc: In function `int* g()':
addrLocal.cc:9: warning: address of local variable `b' returned
[dcb@localhost src]$

Note only one warning message.  
G++ 333 pre release version 2 doesn't find the problem in function f.  
G++ 332 also doesn't find the problem.

Here is Intel C++ doing what I want.

[dcb@localhost src]$ icc -c addrLocal.cc
addrLocal.cc(6): warning #1251: returning pointer to local variable
        return &a;
               ^

addrLocal.cc(13): warning #1251: returning pointer to local variable
        return &b;
               ^

This bug report boiled down from real code in Fedora Core 1.

*/
Comment 1 Andrew Pinski 2004-02-16 12:17:07 UTC
Confirmed, this is an easy fix.
Checking also for PARM_DECL is what is needed:
Index: c-typeck.c
===============================================================
====
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.277
diff -u -p -r1.277 c-typeck.c
--- c-typeck.c  12 Feb 2004 19:14:05 -0000      1.277
+++ c-typeck.c  16 Feb 2004 12:14:54 -0000
@@ -6207,7 +6207,8 @@ c_expand_return (tree retval)
              while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
                inner = TREE_OPERAND (inner, 0);
 
-             if (TREE_CODE (inner) == VAR_DECL
+             if ((TREE_CODE (inner) == VAR_DECL
+                  || TREE_CODE (inner) == PARM_DECL)
                  && ! DECL_EXTERNAL (inner)
                  && ! TREE_STATIC (inner)
                  && DECL_CONTEXT (inner) == current_function_decl)
Index: typeck.c
===============================================================
====
RCS file: /cvs/gcc/gcc/gcc/cp/typeck.c,v
retrieving revision 1.528
diff -u -p -r1.528 typeck.c
--- typeck.c    14 Feb 2004 00:49:12 -0000      1.528
+++ typeck.c    16 Feb 2004 12:16:14 -0000
@@ -5884,7 +5884,8 @@ maybe_warn_about_returning_address_of_lo
        }
     }
 
-  if (TREE_CODE (whats_returned) == VAR_DECL
+  if ((TREE_CODE (whats_returned) == VAR_DECL
+       || TREE_CODE (whats_returned) == PARM_DECL)
       && DECL_NAME (whats_returned)
       && DECL_FUNCTION_SCOPE_P (whats_returned)
       && !(TREE_STATIC (whats_returned)
Comment 2 Andrew Pinski 2004-02-16 12:36:44 UTC
Patch here: http://gcc.gnu.org/ml/gcc-patches/2004-02/msg01407.html.
Comment 3 Andrew Pinski 2004-02-16 13:18:01 UTC
New patch which also fixes it for returning fields parts of structs:
http://gcc.gnu.org/ml/gcc-patches/2004-02/msg01412.html

This will now warn:
struct ll
{
  int i;
};
int *h(struct ll c)
{
  return &c.i;/* { dg-warning "address" "" } */
}
Comment 4 CVS Commits 2004-02-23 15:22:16 UTC
Subject: Bug 14156

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	pinskia@gcc.gnu.org	2004-02-23 15:22:12

Modified files:
	gcc            : ChangeLog c-typeck.c 
	gcc/cp         : ChangeLog typeck.c 

Log message:
	PR c/14156
	* c-typeck.c (c_expand_return): Change check for VAR_DECL
	to use DECL_P instead.
	
	PR c++/14156
	* typeck.c (maybe_warn_about_returning_address_of_location):
	Change check for VAR_DECL to use DECL_P instead.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.2892&r2=2.2893
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-typeck.c.diff?cvsroot=gcc&r1=1.278&r2=1.279
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3967&r2=1.3968
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck.c.diff?cvsroot=gcc&r1=1.528&r2=1.529

Comment 5 CVS Commits 2004-02-23 15:27:51 UTC
Subject: Bug 14156

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	pinskia@gcc.gnu.org	2004-02-23 15:27:49

Modified files:
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.dg: 20040223-1.c 

Log message:
	2004-02-23  Andrew Pinski  <pinskia@physics.uc.edu>
	
	PR c/14156
	* gcc.dg/20040223-1.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.3533&r2=1.3534
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20040223-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 6 Andrew Pinski 2004-02-23 15:29:09 UTC
Fixed for 3.5.0 (will have to revisit when I change the front-ends for the tree-ssa though but I already 
have a patch for it).
Comment 7 d.binderman 2004-02-23 21:00:38 UTC
>Fixed for 3.5.0

The value of your patch, especially to the Redhat Fedora team,
would be considerably increased if it could be available in a sooner
version than 3.5, which is probably a couple years away.

Patch possible for 3.3.4 ?
 
Comment 8 Andrew Pinski 2004-02-23 21:36:08 UTC
I am not going to deal with this junk, this never worked so I am closing this as fixed for 3.5.0 and if you 
want it in for 3.3.4 or 3.4.0, please ask for yourself.
Comment 9 Wolfgang Bangerth 2004-02-23 22:41:32 UTC
3.3.4 will almost certainly not get this patch, since it is not 
a regression. 
 
Regarding 3.4: this is the RM's call. 
 
W. 
Comment 10 Andrew Pinski 2004-03-11 05:45:44 UTC
*** Bug 14519 has been marked as a duplicate of this bug. ***