Skip to content

Commit e520f2e

Browse files
committed
require custom do_php_cli() argv to be passed as utf8
1 parent ef8db59 commit e520f2e

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

‎UPGRADING.INTERNALS‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,4 @@ PHP 8.6 INTERNALS UPGRADE NOTES
366366
__thread storage on ZTS builds.
367367

368368
- php-cli functionality is now available in embed builds via the do_php_cli()
369-
function.
369+
function. On Windows, custom argv must use UTF-8.

‎sapi/cli/cli.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef struct php_cli_server_context {
5555
} php_cli_server_context;
5656

5757
/* this performs full cli-SAPI boot, loads modules, sets up TSRM and co. */
58+
/* On Windows, custom argv strings must be UTF-8. */
5859
extern PHP_CLI_API int do_php_cli(int argc, char *argv[]);
5960

6061
#endif /* CLI_H */

‎sapi/cli/php_cli.c‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,9 +1187,8 @@ PHP_CLI_API int do_php_cli(int argc, char *argv[])
11871187
{
11881188
#if defined(PHP_WIN32)
11891189
int num_args;
1190-
wchar_t **argv_wide;
1190+
wchar_t **argv_wide = NULL;
11911191
char **argv_save = argv;
1192-
BOOL using_wide_argv = 0;
11931192
#endif
11941193

11951194
int c;
@@ -1341,7 +1340,26 @@ PHP_CLI_API int do_php_cli(int argc, char *argv[])
13411340
we can access the internal charset information from PHP. */
13421341
argv_wide = CommandLineToArgvW(GetCommandLineW(), &num_args);
13431342
PHP_WIN32_CP_W_TO_ANY_ARRAY(argv_wide, num_args, argv, argc)
1344-
using_wide_argv = 1;
1343+
} else if (!php_win32_cp_use_unicode()) {
1344+
/* Custom arguments are UTF-8, regardless of PHP's configured encoding. */
1345+
char **converted_argv = calloc((size_t) argc + 1, sizeof(char *));
1346+
if (!converted_argv) {
1347+
exit_status = 1;
1348+
goto out;
1349+
}
1350+
argv = converted_argv;
1351+
for (int i = 0; i < argc; i++) {
1352+
wchar_t *arg = php_win32_cp_utf8_to_w(argv_save[i]);
1353+
if (arg) {
1354+
argv[i] = php_win32_cp_w_to_any(arg);
1355+
free(arg);
1356+
}
1357+
if (!argv[i]) {
1358+
fprintf(stderr, "Could not convert command line argument %d.\n", i);
1359+
exit_status = 1;
1360+
goto out;
1361+
}
1362+
}
13451363
}
13461364

13471365
SetConsoleCtrlHandler(php_cli_win32_ctrl_handler, TRUE);
@@ -1381,8 +1399,10 @@ PHP_CLI_API int do_php_cli(int argc, char *argv[])
13811399
#if defined(PHP_WIN32)
13821400
(void)php_win32_cp_cli_restore();
13831401

1384-
if (using_wide_argv) {
1402+
if (argv != argv_save) {
13851403
PHP_WIN32_CP_FREE_ARRAY(argv, argc);
1404+
}
1405+
if (argv_wide) {
13861406
LocalFree(argv_wide);
13871407
}
13881408
argv = argv_save;

0 commit comments

Comments
 (0)