Skip to content

Commit 53a0269

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)
1 parent 5731a40 commit 53a0269

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
@@ -1830,7 +1830,6 @@ int main(int argc, char *argv[])
18301830
int status = 0;
18311831
#endif
18321832
char *query_string;
1833-
char *decoded_query_string;
18341833
int skip_getopt = 0;
18351834

18361835
#ifdef HAVE_SIGNAL_H
@@ -1886,10 +1885,15 @@ int main(int argc, char *argv[])
18861885
* the executable. Ideally we skip argument parsing when we're in cgi or fastcgi mode,
18871886
* but that breaks PHP scripts on Linux with a hashbang: `#!/php-cgi -d option=value`.
18881887
* Therefore, this code only prevents passing arguments if the query string starts with a '-'.
1889-
* Similarly, scripts spawned in subprocesses on Windows may have the same issue. */
1888+
* Similarly, scripts spawned in subprocesses on Windows may have the same issue.
1889+
* However, Windows has lots of conversion rules and command line parsing rules that
1890+
* are too difficult and dangerous to reliably emulate. */
18901891
if((query_string = getenv("QUERY_STRING")) != NULL && strchr(query_string, '=') == NULL) {
1892+
#ifdef PHP_WIN32
1893+
skip_getopt = cgi || fastcgi;
1894+
#else
18911895
unsigned char *p;
1892-
decoded_query_string = strdup(query_string);
1896+
char *decoded_query_string = strdup(query_string);
18931897
php_url_decode(decoded_query_string, strlen(decoded_query_string));
18941898
for (p = (unsigned char *)decoded_query_string; *p && *p <= ' '; p++) {
18951899
/* skip all leading spaces */
@@ -1898,22 +1902,8 @@ int main(int argc, char *argv[])
18981902
skip_getopt = 1;
18991903
}
19001904

1901-
/* On Windows we have to take into account the "best fit" mapping behaviour. */
1902-
#ifdef PHP_WIN32
1903-
if (*p >= 0x80) {
1904-
wchar_t wide_buf[1];
1905-
wide_buf[0] = *p;
1906-
char char_buf[4];
1907-
size_t wide_buf_len = sizeof(wide_buf) / sizeof(wide_buf[0]);
1908-
size_t char_buf_len = sizeof(char_buf) / sizeof(char_buf[0]);
1909-
if (WideCharToMultiByte(CP_ACP, 0, wide_buf, wide_buf_len, char_buf, char_buf_len, NULL, NULL) == 0
1910-
|| char_buf[0] == '-') {
1911-
skip_getopt = 1;
1912-
}
1913-
}
1914-
#endif
1915-
19161905
free(decoded_query_string);
1906+
#endif
19171907
}
19181908

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

0 commit comments

Comments
 (0)