On Mon, Aug 17, 2020 at 12:08 PM Paul Eggert <egg...@cs.ucla.edu> wrote: > > On 8/17/20 7:40 AM, Bernhard Voelker wrote: > > Currently, we can only get avoid the warning via GCC_LINT. > > Do you have another idea how to avoid it? E.g. referencing > > static memory instead of stack memory? > > Static memory wouldn't be thread-safe or reentrant. I don't see any easy > alternative, other than always using malloc but this function gets used quite > a > bit in some apps. > > What's wrong with avoiding the problem via GCC_LINT? > > I suppose you could add -Wno-return-local-addr to your CFLAGS, but I don't > recommend this as it's normally a useful warning.
I observed a few of those warnings in other places, too. I think the easiest way to work around a false positive is something like this in a source (*.c) file: // Need GCC 4.6/Clang 1.7/Apple Clang 2.0 or above due to "GCC diagnostic {push|pop}" #if (GCC_VERSION >= 40600) || (LLVM_CLANG_VERSION >= 10700) || \ (APPLE_CLANG_VERSION >= 20000) #define GCC_DIAGNOSTIC_AVAILABLE 1 #endif #if GCC_DIAGNOSTIC_AVAILABLE # pragma GCC diagnostic ignored "-Wreturn-local-addr" #endif If a header file is triggering the warning, then you have to push/pop around the function body: #if GCC_DIAGNOSTIC_AVAILABLE # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wreturn-local-addr" #endif inline const char* some_func(...) { ... } #if GCC_DIAGNOSTIC_AVAILABLE # pragma GCC diagnostic pop #endif The upside of using the diagnostic pragma is, (1) it avoids noisy compiles (a quality/security gate), (2) it fixes it once for everyone, and (3) it halts bug reports and mailing list messages. The downside is, (1) it is uglifies the code and (2) only applies to modern compilers. I don't worry too much about the modern compiler requirement. Anyone using GCC 3 for regression testing is not in a standard use case. They are a one-off and will have to accept the noise. Jeff