Collin Funk wrote:
> The putenv
> function places the pointer that it is given into the environment.
> When it goes out of scope the behavior is undefined [1].

Yes. Practically this means that the code in the exit() function
(which gets executed after main() returns) may call getenv(), and
this can crash when it encounters strings that were allocated on
the stack (that has meanwhile been unwound).

> I think this change may have uncovered a GCC bug? I noticed lots of
> -Wanalyzer-putenv-of-auto-var spam in testdirs.

It is not spam. It is fully justified, since in
  putenv ((char []) {"TEST_VAR=abc"})
the argument is allocated in automatic storage. See ISO C § 6.5.2.5.(12).

> When I checkout the commit that I added these tests and run:
> 
>     $ git checkout 259dd4a0655eb9b6cd2adead0934c6ee046a2dac
>     $ gnulib-tool --create-testdir --dir testdir1 putenv
>     $ ./configure CFLAGS="-fanalyzer"
>     $ make
> 
> I see no warnings for that file.

This could warn for the line

    static char *var = "TEST_VAR=abc";

depending on the warning options enabled. Namely, it warns when
-Wwrite-strings is enabled:

  warning: initialization discards ‘const’ qualifier from pointer target type 
[-Wdiscarded-qualifiers]

> I believe the warning should be applied in both cases.

No. The analyzer's warning is only about strings with limited lifetime,
it is not about strings that are not writable. That is a different
category of warnings.

Bruno




Reply via email to