Hi Paul,

On 5/16/24 10:42 PM, Paul Eggert wrote:
> diff --git a/tests/test-putenv.c b/tests/test-putenv.c
> index 1768e7563a..564c86713a 100644
> --- a/tests/test-putenv.c
> +++ b/tests/test-putenv.c
> @@ -39,7 +39,7 @@ main (void)
>  
>    /* Verify adding an environment variable.  */
>    {
> -    ASSERT (putenv ("TEST_VAR=abc") == 0);
> +    ASSERT (putenv ((char []) {"TEST_VAR=abc"}) == 0);

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

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. When I checkout your commit:

    $ git checkout 15cd8edb6ec9aed2585e10456d46eec09d5c1b8b

and run the same commands I see the spam again:

======================================================
test-putenv.c:56:13: warning: ‘putenv’ on a pointer to automatic variable 
‘<U6630>’ [POS34-C] [-Wanalyzer-putenv-of-auto-var]
   56 |     ASSERT (putenv ((char []) {"TEST_VAR"}) == 0);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
macros.h:57:13: note: in definition of macro ‘ASSERT’
   57 |       if (!(expr))                                                      
     \
      |             ^~~~
  ‘main’: event 1
    |
    |   57 |       if (!(expr))                                                 
          \
    |      |          ^
    |      |          |
    |      |          (1) following ‘false’ branch...
======================================================

I believe the warning should be applied in both cases. The putenv
function places the pointer that it is given into the environment.
When it goes out of scope the behavior is undefined [1].

Looking at your commit b98993a1baaa2fc39b301676ecbd8bb29e1d9c96 [2]:

diff --git a/tests/test-unsetenv.c b/tests/test-unsetenv.c
index ddc412867f..d8e5b01192 100644
--- a/tests/test-unsetenv.c
+++ b/tests/test-unsetenv.c
@@ -32,7 +32,8 @@ SIGNATURE_CHECK (unsetenv, int, (char const *));
 int
 main (void)
 {
-  char entry[] = "b=2";
+  /* Static to pacify gcc -Wanalyzer-putenv-of-auto-var.  */
+  static char entry[] = "b=2";
 
   /* Test removal when multiple entries present.  */
   ASSERT (putenv ((char *) "a=1") == 0);
   ASSERT (putenv (entry) == 0);

The warning isn't very important since 'entry' never leaves scope.
However if that causes a warning why doesn't this line:

   ASSERT (putenv ((char *) "a=1") == 0);

I'm assuming the analyzer doesn't handle the types correctly. Bug
report time for me. :)

[1] 
https://wiki.sei.cmu.edu/confluence/display/c/POS34-C.+Do+not+call+putenv%28%29+with+a+pointer+to+an+automatic+variable+as+the+argument
[2] 
https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=commit;h=b98993a1baaa2fc39b301676ecbd8bb29e1d9c96

Collin

Reply via email to