5

I have the following test code, file test.c:

#include <stdio.h>

int *func()
{
    int i = 123;
    return &i;
}

int main()
{
    printf("%d\n", *func());
}

If I use the command to compile it that is OK,

gcc test.c -o test

It will have the following warning information:

warning: address of stack memory associated with local variable 'i'
  returned [-Wreturn-stack-address]
return &i;
        ^
1 warning generated.

But it can output the result: 123

If I use the command:

gcc -Werror test.c -o test

It will have the following error information:

error: address of stack memory associated with local variable 'i'
  returned [-Werror,-Wreturn-stack-address]
return &i;
        ^
1 error generated.

Now I want to use the -Werror option, but I also want to ignore the address of stack memory associated with local variable 'i' warning. What should I do?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
thinkerou
  • 1,781
  • 5
  • 17
  • 28
  • 1
    You realize that returning a pointer to stack variables is a terrible idea, right? If you make any function calls at all (including implicit function calls, sometimes performed when you initialize a struct/array with `= {0};` for instance), the value referenced by the pointer will no longer be valid. This is a warning for a reason. – ShadowRanger Aug 09 '16 at 01:20
  • Thanks @ShadowRanger I got it, I only want to resolve it temporary. – thinkerou Aug 09 '16 at 01:28
  • Some canonicals for the error you can not ignore - as said in an answer (what is the point of getting rid of a warning if the result is almost certainly a crash at runtime?): – Peter Mortensen Oct 22 '22 at 05:14
  • Same error message: *[Using C-string gives Warning: "Address of stack memory associated with local variable returned"](https://stackoverflow.com/questions/18041100/using-c-string-gives-warning-address-of-stack-memory-associated-with-local-var)* – Peter Mortensen Oct 22 '22 at 05:20
  • *[C++ Returning reference to local variable](https://stackoverflow.com/questions/4643713/c-returning-reference-to-local-variable)* (2011, five answers and 137 upvotes). – Peter Mortensen Oct 22 '22 at 05:20
  • *[Returning an array using C](https://stackoverflow.com/questions/11656532/)* (2012, 12 answers, and 196 upvotes) – Peter Mortensen Oct 22 '22 at 05:20
  • The result is different in newer versions of GCC. Both 7.5.0 (7.4 released 2018-12-06) and 9.4 (released 2021-06-01): `warning: function returns address of local variable [-Wreturn-local-addr]`. For `&` in `return &i;`. It was tested on Linux / Ubuntu ([18.04](https://en.wikipedia.org/wiki/Ubuntu_version_history#Ubuntu_18.04_LTS_.28Bionic_Beaver.29) and [20.04](https://en.wikipedia.org/wiki/Ubuntu_version_history#Ubuntu_20.04_LTS_(Focal_Fossa))). – Peter Mortensen Oct 22 '22 at 20:48
  • What version of GCC? Running on what platform? – Peter Mortensen Oct 22 '22 at 20:49
  • ***I don't think the OP has used [GCC](https://en.wikipedia.org/wiki/GNU_Compiler_Collection)***, but [rather Clang instead](https://stackoverflow.com/questions/38840601/how-can-i-ignore-an-error-when-using-gcc-compile-option-werror/38840652#comment130977715_38840652). [`-Wreturn-stack-address`](https://github.com/Barro/compiler-warnings/blob/master/clang/warnings-clang-3.3.txt) is a characteristic of [Clang](https://en.wikipedia.org/wiki/Clang). It does not exist in GCC (unless it was removed before GCC 3.4 (2003 or earlier) - very unlikely). – Peter Mortensen Oct 24 '22 at 17:02
  • The result with [Clang](https://en.wikipedia.org/wiki/Clang) version 6.0 on [Ubuntu 18.04](https://en.wikipedia.org/wiki/Ubuntu_version_history#Ubuntu_18.04_LTS_.28Bionic_Beaver.29) (Bionic Beaver) was the *exact* same as reported in the question. – Peter Mortensen Oct 25 '22 at 16:52
  • The result with [Clang](https://en.wikipedia.org/wiki/Clang) version 6.0 on [Ubuntu 18.04](https://en.wikipedia.org/wiki/Ubuntu_version_history#Ubuntu_18.04_LTS_.28Bionic_Beaver.29) (Bionic Beaver) was the *exact* same as reported in the question: `clang SO38840601.c -o test`. Perhaps `gcc` is an alias to `clang` on the OP's system? Is that a common thing? – Peter Mortensen Oct 25 '22 at 16:55
  • [A documentation entry](https://clang.llvm.org/docs/DiagnosticsReference.html#wreturn-stack-address) for "-Wreturn-stack-address" in Clang. – Peter Mortensen Oct 25 '22 at 17:03
  • Another signature of Clang is *"1 warning generated."* (GCC doesn't output anything in that regard). This is strengthened by the output for the `-Werror` case: Clang outputs "`1 error generated`" whereas GCC outputs "`cc1: all warnings being treated as errors`". – Peter Mortensen Oct 26 '22 at 13:45
  • I think we can conclude the OP had the executable `gcc` aliased (or similar) to the executable for the [Clang](https://en.wikipedia.org/wiki/Clang) compiler (most likely the executable `clang`). – Peter Mortensen Oct 26 '22 at 13:49
  • (Actually using GCC (version 7.5.0) to compile the program (without '-Werror'), and [on Linux](https://en.wikipedia.org/wiki/Ubuntu_version_history#Ubuntu_18.04_LTS_.28Bionic_Beaver.29), running it resulted in *"`Segmentation fault (core dumped)`"*.) – Peter Mortensen Oct 29 '22 at 19:37
  • Here is a hint regarding the aliasing of executable `gcc` to the [Clang](https://en.wikipedia.org/wiki/Clang) compiler: *[Why does the `gcc` command on macOS execute `clang`?](https://www.quora.com/Why-does-the-gcc-command-on-MacOS-execute-clang?share=1)*. Due to [licensing issues](https://www.quora.com/Is-Apple-supporting-Clang-in-order-to-destroy-GCC/answer/Mario-Ray-Mahardhika-1)? – Peter Mortensen Nov 08 '22 at 18:32
  • Was it on a Mac? – Peter Mortensen Nov 08 '22 at 18:33
  • What was your platform (e.g., Linux distribution, incl. versions) and configuration (e.g. of compilers, incl. versions)? – Peter Mortensen Aug 31 '23 at 11:50

1 Answers1

8

Most gcc warnings can be disabled by prefixing the name of the warning with no-, e.g. -Wno-return-stack-address.

That said, this is not something you want to ignore; returning pointers to stack variables is undefined behavior, and while it has semi-predictable results on most compilers, it's incredibly fragile; any function call at all, implicit or explicit, could stomp on the value that pointer is referencing.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • [In a newer version of GCC it would](https://stackoverflow.com/questions/38840601/how-can-i-ignore-an-error-when-using-gcc-compile-option-werror#comment130946668_38840601) be [`-Wno-return-local-addr`](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wno-return-local-addr) instead of `-Wno-return-stack-address` – Peter Mortensen Oct 22 '22 at 20:55
  • What is the latest version of GCC to support `-Wno-return-stack-address`? (I can't find it anywhere in the documentation for the current version). That would put version constraints on what the OP was using. Search engines are very unwilling to reveal this information. Is there a more efficient technique when it comes to navigating the GCC documentation, incl. older versions? – Peter Mortensen Oct 22 '22 at 21:02
  • @PeterMortensen: No idea. The OP told me what the warning switch was, I told them to add `no-` to the name to disable it. – ShadowRanger Oct 22 '22 at 21:36
  • OK, I found [a more efficient search method](https://github.com/barro/compiler-warnings#gcc-warning-flags). – Peter Mortensen Oct 24 '22 at 16:15
  • A search should be *without* the three letters "-no" (or use just the last part). `-Wreturn-local-addr` was [apparently introduced with GCC 4.8](https://github.com/Barro/compiler-warnings/blob/master/gcc/warnings-gcc-4.8.txt) ([was released](https://gcc.gnu.org/releases.html) 2015-06-23). It wasn't in [GCC 4.7](https://github.com/Barro/compiler-warnings/blob/master/gcc/warnings-gcc-4.7.txt). – Peter Mortensen Oct 24 '22 at 16:45
  • The OP has probably ***not*** used GCC, but some other compiler (with *some* common option names). `-Wreturn-stack-address` seems to have been [introduced in Clang 3.3](https://github.com/Barro/compiler-warnings/blob/master/clang/warnings-clang-3.3.txt) and is in the latest version (not listed for [3.2](https://github.com/Barro/compiler-warnings/blob/master/clang/warnings-clang-3.2.txt)). Clang 3.3 was released before 2015, [some time in 2012 - 2014](https://en.wikipedia.org/wiki/Clang#Status_history). – Peter Mortensen Oct 24 '22 at 16:52
  • No wonder it was so difficult to find!!! – Peter Mortensen Oct 24 '22 at 17:07
  • @PeterMortensen: I'm pretty sure I've seen some weirdo systems that aliased `gcc` to `clang`, so I wouldn't be surprised if the OP legit thought they were using `gcc` when they were not. – ShadowRanger Oct 24 '22 at 18:42