Skip to content

Commit 4b9cd27

Browse files
nielsdosbukka
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.
1 parent d65a1e6 commit 4b9cd27

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
@@ -1748,7 +1748,6 @@ int main(int argc, char *argv[])
17481748
int status = 0;
17491749
#endif
17501750
char *query_string;
1751-
char *decoded_query_string;
17521751
int skip_getopt = 0;
17531752

17541753
#if defined(SIGPIPE) && defined(SIG_IGN)
@@ -1803,10 +1802,15 @@ int main(int argc, char *argv[])
18031802
* the executable. Ideally we skip argument parsing when we're in cgi or fastcgi mode,
18041803
* but that breaks PHP scripts on Linux with a hashbang: `#!/php-cgi -d option=value`.
18051804
* Therefore, this code only prevents passing arguments if the query string starts with a '-'.
1806-
* Similarly, scripts spawned in subprocesses on Windows may have the same issue. */
1805+
* Similarly, scripts spawned in subprocesses on Windows may have the same issue.
1806+
* However, Windows has lots of conversion rules and command line parsing rules that
1807+
* are too difficult and dangerous to reliably emulate. */
18071808
if((query_string = getenv("QUERY_STRING")) != NULL && strchr(query_string, '=') == NULL) {
1809+
#ifdef PHP_WIN32
1810+
skip_getopt = cgi || fastcgi;
1811+
#else
18081812
unsigned char *p;
1809-
decoded_query_string = strdup(query_string);
1813+
char *decoded_query_string = strdup(query_string);
18101814
php_url_decode(decoded_query_string, strlen(decoded_query_string));
18111815
for (p = (unsigned char *)decoded_query_string; *p && *p <= ' '; p++) {
18121816
/* skip all leading spaces */
@@ -1815,22 +1819,8 @@ int main(int argc, char *argv[])
18151819
skip_getopt = 1;
18161820
}
18171821

1818-
/* On Windows we have to take into account the "best fit" mapping behaviour. */
1819-
#ifdef PHP_WIN32
1820-
if (*p >= 0x80) {
1821-
wchar_t wide_buf[1];
1822-
wide_buf[0] = *p;
1823-
char char_buf[4];
1824-
size_t wide_buf_len = sizeof(wide_buf) / sizeof(wide_buf[0]);
1825-
size_t char_buf_len = sizeof(char_buf) / sizeof(char_buf[0]);
1826-
if (WideCharToMultiByte(CP_ACP, 0, wide_buf, wide_buf_len, char_buf, char_buf_len, NULL, NULL) == 0
1827-
|| char_buf[0] == '-') {
1828-
skip_getopt = 1;
1829-
}
1830-
}
1831-
#endif
1832-
18331822
free(decoded_query_string);
1823+
#endif
18341824
}
18351825

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

0 commit comments

Comments
 (0)