Skip to content

Commit 56f2434

Browse files
nielsdosremicollet
authored andcommitted
It's no use trying to work around whatever the operating system and Apache do because we'll be fighting that until eternity. Change the skip_getopt condition such that when we're running in CGI or FastCGI mode we always skip the argument parsing. This is a BC break, but this seems to be the only way to get rid of this class of issues. (cherry picked from commit abcfd980bfa03298792fd3aba051c78d52f10642) (cherry picked from commit 2d2552e) (cherry picked from commit 1158d06) (cherry picked from commit 89c6677) (cherry picked from commit 53a0269)
1 parent c9e67e9 commit 56f2434

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

sapi/cgi/cgi_main.c

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,6 @@ int main(int argc, char *argv[])
18261826
int status = 0;
18271827
#endif
18281828
char *query_string;
1829-
char *decoded_query_string;
18301829
int skip_getopt = 0;
18311830

18321831
#if 0 && defined(PHP_DEBUG)
@@ -1892,10 +1891,15 @@ int main(int argc, char *argv[])
18921891
* the executable. Ideally we skip argument parsing when we're in cgi or fastcgi mode,
18931892
* but that breaks PHP scripts on Linux with a hashbang: `#!/php-cgi -d option=value`.
18941893
* Therefore, this code only prevents passing arguments if the query string starts with a '-'.
1895-
* Similarly, scripts spawned in subprocesses on Windows may have the same issue. */
1894+
* Similarly, scripts spawned in subprocesses on Windows may have the same issue.
1895+
* However, Windows has lots of conversion rules and command line parsing rules that
1896+
* are too difficult and dangerous to reliably emulate. */
18961897
if((query_string = getenv("QUERY_STRING")) != NULL && strchr(query_string, '=') == NULL) {
1898+
#ifdef PHP_WIN32
1899+
skip_getopt = cgi || fastcgi;
1900+
#else
18971901
unsigned char *p;
1898-
decoded_query_string = strdup(query_string);
1902+
char *decoded_query_string = strdup(query_string);
18991903
php_url_decode(decoded_query_string, strlen(decoded_query_string));
19001904
for (p = (unsigned char *)decoded_query_string; *p && *p <= ' '; p++) {
19011905
/* skip all leading spaces */
@@ -1904,22 +1908,8 @@ int main(int argc, char *argv[])
19041908
skip_getopt = 1;
19051909
}
19061910

1907-
/* On Windows we have to take into account the "best fit" mapping behaviour. */
1908-
#ifdef PHP_WIN32
1909-
if (*p >= 0x80) {
1910-
wchar_t wide_buf[1];
1911-
wide_buf[0] = *p;
1912-
char char_buf[4];
1913-
size_t wide_buf_len = sizeof(wide_buf) / sizeof(wide_buf[0]);
1914-
size_t char_buf_len = sizeof(char_buf) / sizeof(char_buf[0]);
1915-
if (WideCharToMultiByte(CP_ACP, 0, wide_buf, wide_buf_len, char_buf, char_buf_len, NULL, NULL) == 0
1916-
|| char_buf[0] == '-') {
1917-
skip_getopt = 1;
1918-
}
1919-
}
1920-
#endif
1921-
19221911
free(decoded_query_string);
1912+
#endif
19231913
}
19241914

19251915
while (!skip_getopt && (c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {

0 commit comments

Comments
 (0)