Skip to content

Commit 493b498

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) (cherry picked from commit 56f2434)
1 parent cc487bc commit 493b498

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
@@ -1742,7 +1742,6 @@ int main(int argc, char *argv[])
17421742
int status = 0;
17431743
#endif
17441744
char *query_string;
1745-
char *decoded_query_string;
17461745
int skip_getopt = 0;
17471746

17481747
#if 0 && defined(PHP_DEBUG)
@@ -1810,10 +1809,15 @@ int main(int argc, char *argv[])
18101809
* the executable. Ideally we skip argument parsing when we're in cgi or fastcgi mode,
18111810
* but that breaks PHP scripts on Linux with a hashbang: `#!/php-cgi -d option=value`.
18121811
* Therefore, this code only prevents passing arguments if the query string starts with a '-'.
1813-
* Similarly, scripts spawned in subprocesses on Windows may have the same issue. */
1812+
* Similarly, scripts spawned in subprocesses on Windows may have the same issue.
1813+
* However, Windows has lots of conversion rules and command line parsing rules that
1814+
* are too difficult and dangerous to reliably emulate. */
18141815
if((query_string = getenv("QUERY_STRING")) != NULL && strchr(query_string, '=') == NULL) {
1816+
#ifdef PHP_WIN32
1817+
skip_getopt = cgi || fastcgi;
1818+
#else
18151819
unsigned char *p;
1816-
decoded_query_string = strdup(query_string);
1820+
char *decoded_query_string = strdup(query_string);
18171821
php_url_decode(decoded_query_string, strlen(decoded_query_string));
18181822
for (p = (unsigned char *)decoded_query_string; *p && *p <= ' '; p++) {
18191823
/* skip all leading spaces */
@@ -1822,22 +1826,8 @@ int main(int argc, char *argv[])
18221826
skip_getopt = 1;
18231827
}
18241828

1825-
/* On Windows we have to take into account the "best fit" mapping behaviour. */
1826-
#ifdef PHP_WIN32
1827-
if (*p >= 0x80) {
1828-
wchar_t wide_buf[1];
1829-
wide_buf[0] = *p;
1830-
char char_buf[4];
1831-
size_t wide_buf_len = sizeof(wide_buf) / sizeof(wide_buf[0]);
1832-
size_t char_buf_len = sizeof(char_buf) / sizeof(char_buf[0]);
1833-
if (WideCharToMultiByte(CP_ACP, 0, wide_buf, wide_buf_len, char_buf, char_buf_len, NULL, NULL) == 0
1834-
|| char_buf[0] == '-') {
1835-
skip_getopt = 1;
1836-
}
1837-
}
1838-
#endif
1839-
18401829
free(decoded_query_string);
1830+
#endif
18411831
}
18421832

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

0 commit comments

Comments
 (0)