Skip to content

Commit

Permalink
Follow up on bug #75574 for FCGI side
Browse files Browse the repository at this point in the history
  • Loading branch information
weltling committed Nov 28, 2017
1 parent 2873316 commit 8b57a5b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
6 changes: 6 additions & 0 deletions main/SAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,12 @@ SAPI_API char *sapi_getenv(char *name, size_t name_len)
char *value, *tmp = sapi_module.getenv(name, name_len);
if (tmp) {
value = estrdup(tmp);
#ifdef PHP_WIN32
if (strlen(sapi_module.name) == sizeof("cgi-fcgi") - 1 && !strcmp(sapi_module.name, "cgi-fcgi")) {
/* XXX more modules to go, if needed. */
free(tmp);
}
#endif
} else {
return NULL;
}
Expand Down
42 changes: 42 additions & 0 deletions sapi/cgi/cgi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,47 @@ static size_t sapi_fcgi_read_post(char *buffer, size_t count_bytes)
return read_bytes;
}

#ifdef PHP_WIN32
/* The result needs to be freed! See sapi_getenv(). */
static char *cgi_getenv_win32(const char *name, size_t name_len)
{
char *ret = NULL;
wchar_t *keyw, *valw;
size_t size;
int rc;

keyw = php_win32_cp_conv_any_to_w(name, name_len, PHP_WIN32_CP_IGNORE_LEN_P);
if (!keyw) {
return NULL;
}

rc = _wgetenv_s(&size, NULL, 0, keyw);
if (rc || 0 == size) {
free(keyw);
return NULL;
}

valw = emalloc((size + 1) * sizeof(wchar_t));

rc = _wgetenv_s(&size, valw, size, keyw);
if (!rc) {
ret = php_win32_cp_w_to_any(valw);
}

free(keyw);
efree(valw);

return ret;
}
#endif

static char *sapi_cgi_getenv(char *name, size_t name_len)
{
#ifndef PHP_WIN32
return getenv(name);
#else
return cgi_getenv_win32(name, name_len);
#endif
}

static char *sapi_fcgi_getenv(char *name, size_t name_len)
Expand All @@ -547,7 +585,11 @@ static char *sapi_fcgi_getenv(char *name, size_t name_len)
if (ret) return ret;
/* if cgi, or fastcgi and not found in fcgi env
check the regular environment */
#ifndef PHP_WIN32
return getenv(name);
#else
return cgi_getenv_win32(name, name_len);
#endif
}

static char *_sapi_cgi_putenv(char *name, size_t name_len, char *value)
Expand Down
35 changes: 35 additions & 0 deletions sapi/cgi/tests/bug75574_utf8_win.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Bug #75574 putenv does not work properly if parameter contains non-ASCII unicode character, UTF-8
--SKIPIF--
<?php

if (substr(PHP_OS, 0, 3) != 'WIN') {
die("skip Valid only on Windows");
}
include "skipif.inc";
?>
--FILE--
<?php
/*
#vim: set fileencoding=utf-8
#vim: set encoding=utf-8
*/

include "include.inc";

$php = get_cgi_path();
reset_env_vars();

$fn = dirname(__FILE__) . DIRECTORY_SEPARATOR . md5(uniqid());
file_put_contents($fn, "<?php\nvar_dump(putenv('FOO=啊'));\n//var_dump(`echo %FOO%`);\nvar_dump(getenv('FOO'));");

echo shell_exec("$php -n -f $fn");

unlink($fn);

?>
===DONE===
--EXPECTF--
bool(true)
string(3) "啊"
===DONE===

0 comments on commit 8b57a5b

Please sign in to comment.