Skip to content

Commit

Permalink
Fix setenv() with value==NULL on Windows
Browse files Browse the repository at this point in the history
Also comment about putenv() taking a *copy* of the string. That is
Windows-specific behaviour and not POSIX-compliant, so we don't want
anyone thinking it's reasonable to do it elsewhere:

Tested thus, under wine and Windows 7:

int main(void)
{
	char foo[]="FOO=bar";
	char *bar;

	putenv(foo);
	printf("FOO is: %s\n", getenv("FOO"));
	foo[5] = 'f';
	printf("FOO is: %s\n", getenv("FOO"));
	putenv("FOO=");
	printf("FOO is: %s\n", getenv("FOO"));
}

With the following results:

FOO is: bar
FOO is: bar
FOO is: (null)

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Feb 11, 2014
1 parent f266381 commit 5dfbffb
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion compat.c
Expand Up @@ -166,7 +166,14 @@ char *openconnect__strcasestr(const char *haystack, const char *needle)
#ifndef HAVE_SETENV
int openconnect__setenv(const char *name, const char *value, int overwrite)
{
char *buf = alloca(strlen(name) + strlen(value) + 2);
char *buf;

if (!value) {
openconnect__unsetenv(name);
return 0;
}
/* Windows putenv() takes a copy of the string */
buf = alloca(strlen(name) + strlen(value) + 2);

sprintf(buf, "%s=%s", name, value);
putenv(buf);
Expand Down

0 comments on commit 5dfbffb

Please sign in to comment.