This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: x86 64 bit function argument bug?


On 04-May-2003, Falk Hueffner <falk.hueffner@student.uni-tuebingen.de> wrote:
> Stephen Biggs <xyzzy@hotpop.com> writes:
> 
> > The following weird 2 lines of C code (yes, I know taking the address of
> > a disappearing entity is an evil thing!):
> > int *i ( int arg ) { return &arg; }
> > long long *ll ( long long arg ) { return &arg; }
> 
> Since it is impossible to use the resulting pointer without invoking
> undefined behaviour, I don't think it is particularly interesting what
> code is generated. We should probably warn about that.

How about the following?

*** c-typeck.c.old	Sun May  4 23:58:00 2003
--- c-typeck.c	Sun May  4 23:58:33 2003
*************** c_expand_return (retval)
*** 7066,7078 ****
  	    case ADDR_EXPR:
  	      inner = TREE_OPERAND (inner, 0);
  
  	      while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
  		inner = TREE_OPERAND (inner, 0);
  
! 	      if (TREE_CODE (inner) == VAR_DECL
  		  && ! DECL_EXTERNAL (inner)
  		  && ! TREE_STATIC (inner)
  		  && DECL_CONTEXT (inner) == current_function_decl)
  		warning ("function returns address of local variable");
  	      break;
  
--- 7066,7079 ----
  	    case ADDR_EXPR:
  	      inner = TREE_OPERAND (inner, 0);
  
  	      while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
  		inner = TREE_OPERAND (inner, 0);
  
! 	      if ((TREE_CODE (inner) == VAR_DECL
! 	           || TREE_CODE (inner) == PARM_DECL)
  		  && ! DECL_EXTERNAL (inner)
  		  && ! TREE_STATIC (inner)
  		  && DECL_CONTEXT (inner) == current_function_decl)
  		warning ("function returns address of local variable");
  	      break;

Actually this is against an old tree, I'm not sure if it applies
against the current tree.  Also the text of the warning should
be different in the case where the function returns the address of
a parameter rather than a local variable.  Also there should be
a test case.

Does anyone feel like picking this up and following through with it?

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]