[Rd] Compiler warning: function returns address of local variable

Radford Neal radford at cs.toronto.edu
Mon Oct 7 13:06:28 CEST 2013


> > ..., my previous
> > dev-lang/R-2.10.1 ebuild package has been upgraded to R-3.0.1.
> > While compiling it, I have got the following compiler warning:
> >
> >   * QA Notice: Package triggers severe warnings which indicate that it
> >   *            may exhibit random runtime failures.
> >   * main.c:1548:5: warning: function returns address of local variable
> > [enabled by default]
> >
> >   * Please do not file a Gentoo bug and instead report the above QA
> >   * issues directly to the upstream developers of this software.
> >   * Homepage: http://www.r-project.org/

> That is the whole point of that piece of C code, so please do report the 
> Gentoo bug (their message is simply bogus) to them.
> 
> And also read posting guide before posting: HTML mail is not allowed here.
> 
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk


The message is certainly not "simply bogus".  Returning the address of
a local variable is almost always a bug.

In this case, there is no real bug in R-3.0.1, since the result is
used for the low-level, system-dependent operation of determining the
direction of stack growth, but this is a very rare use.  For the
compiler to issue such a warning is fully justified.

The warning could be avoided by using the approach taken in pqR, which
has the following routine in main.c:

  /* Detemine whether the stack grows down (+1) or up (-1).  Passed the
     address of a local variable in the caller's stack frame.
  
     This is put here, though called only from system.c, so that the compiler
     will not be able to inline it. */
  
  int R_stack_growth_direction (uintptr_t cvaraddr)
  {
      int dummy;
      return (uintptr_t) &dummy < cvaraddr ? 1 : -1;
  }



More information about the R-devel mailing list