Follow-up: setenv() doesn't export unsetenv()ed variables to environ

YAMAMOTO, Taku taku at tackymt.homeip.net
Sun Sep 9 16:17:40 PDT 2007


Ouch, the testcase had got stripped due to inappropriate MIME type.


On Mon, 10 Sep 2007 08:14:25 +0900
"YAMAMOTO, Taku" <taku at tackymt.homeip.net> wrote:

> Greetings,
> 
> I found a regression in src/lib/libc/stdlib/getenv.c during tracking
> strange behaviour of the sshguard-pf.
> 
> In short, setenv() doesn't add an entry to environ[] if the name was
> once removed by unsetenv().
> 
> I'm suspecting the function __setenv() lacks care of the case to reuse
> a deactivated entry and it needs the following change:
> 
> --- lib/libc/stdlib/getenv.c.orig	2007-07-21 08:30:13.000000000 +0900
> +++ lib/libc/stdlib/getenv.c	2007-09-10 08:07:22.732672106 +0900
> @@ -492,7 +492,7 @@ __setenv(const char *name, size_t nameLe
>  	newEnvActive++;
>  
>  	/* No need to rebuild environ if the variable was reused. */
> -	if (reuse)
> +	if (reuse && newEnvActive == envActive)
>  		return (0);
>  	else
>  		return (__rebuild_environ(newEnvActive));
> 
> 
> A small testcase is attached.
> It should emit:
> expecting "foo": foo
> expecting "bar": bar
> 
> With current broken setenv(), it would emit:
> expecting "foo": foo
> expecting "bar":
> 
> 
> -- 
> -|-__    YAMAMOTO, Taku
>  | __ <     <taku at tackymt.homeip.net>
> 
>       - A chicken is an egg's way of producing more eggs. -
> 


-- 
-|-__   山本 拓 / YAMAMOTO, Taku
 | __ <     <taku at tackymt.homeip.net>

      - A chicken is an egg's way of producing more eggs. -
-------------- next part --------------
#include <err.h>
#include <string.h>
#include <stdlib.h>

int
main(void)
{
	if (setenv("HOGE", "foo", 1))
		warn("setenv(1st)");
	system("echo 'expecting \"foo\":' $HOGE");
	if (unsetenv("HOGE"))
		warn("unsetenv(1st)");
	if (setenv("HOGE", "bar", 1))
		warn("setenv(2nd)");
	system("echo 'expecting \"bar\":' $HOGE");
	return 0;
}


More information about the freebsd-current mailing list