Skip to content

Commit 65efafb

Browse files
committed
create embed test, verify argv always passed as valid utf8
1 parent e520f2e commit 65efafb

5 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎sapi/cli/php_cli.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,18 @@ PHP_CLI_API int do_php_cli(int argc, char *argv[])
12071207
*/
12081208
argv = save_ps_args(argc, argv);
12091209

1210+
#ifdef PHP_WIN32
1211+
if (argv_save != __argv) {
1212+
for (int i = 0; i < argc; i++) {
1213+
if (!MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, argv[i], -1, NULL, 0)) {
1214+
fprintf(stderr, "Invalid UTF-8 in command line argument %d.\n", i);
1215+
cleanup_ps_args(argv);
1216+
return 1;
1217+
}
1218+
}
1219+
}
1220+
#endif
1221+
12101222
#if defined(PHP_WIN32) && !defined(PHP_CLI_WIN32_NO_CONSOLE)
12111223
php_win32_console_fileno_set_vt100(STDOUT_FILENO, TRUE);
12121224
php_win32_console_fileno_set_vt100(STDERR_FILENO, TRUE);

‎sapi/embed/Makefile.frag.w32‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test_helpers: $(BUILD_DIR)\php_embed_cli_test.exe
2+
3+
$(BUILD_DIR)\sapi\embed\cli_argv.obj: $(PHP_SRC_DIR)\sapi\embed\tests\cli_argv.c
4+
cd $(PHP_SRC_DIR)
5+
$(CC) $(CFLAGS_EMBED_OBJ) $(CFLAGS) $(CFLAGS_BD_SAPI_EMBED) /Fo$@ /c $(PHP_SRC_DIR)\sapi\embed\tests\cli_argv.c
6+
7+
$(BUILD_DIR)\php_embed_cli_test.exe: $(BUILD_DIR)\sapi\embed\cli_argv.obj $(BUILD_DIR)\$(EMBED_LIB)
8+
"$(LINK)" /nologo $(BUILD_DIR)\sapi\embed\cli_argv.obj $(BUILD_DIR)\$(EMBED_LIB) /out:$@ $(LDFLAGS) $(LDFLAGS_EMBED) /stack:67108864

‎sapi/embed/config.w32‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ if (PHP_EMBED != "no") {
1010
ADD_FLAG("LIBS_EMBED", "ws2_32.lib");
1111
ADD_FLAG("LIBS_EMBED", "shell32.lib");
1212
PHP_INSTALL_HEADERS("sapi/embed", "php_embed.h");
13+
DEFINE('EMBED_LIB', 'php' + PHP_VERSION + 'embed.lib');
14+
ADD_MAKEFILE_FRAGMENT();
1315
}

‎sapi/embed/tests/cli_argv.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "php.h"
2+
#include "sapi/cli/cli.h"
3+
4+
int main(int argc, char *argv[])
5+
{
6+
char *php_argv[] = {
7+
"embedded-php", "-n", "-d", "default_charset=UTF-8", "-r",
8+
"echo json_encode([$argc, array_map('bin2hex', array_slice($argv, 1))]), PHP_EOL; exit(23);",
9+
"--", "caf\xc3\xa9", "argument with spaces", "", "\xf0\x9f\x98\x80", NULL
10+
};
11+
if (argc > 1 && strcmp(argv[1], "default-charset") == 0) {
12+
php_argv[3] = "default_charset=Windows-1252";
13+
php_argv[10] = "\xe2\x82\xac";
14+
} else if (argc > 1 && strcmp(argv[1], "internal-encoding") == 0) {
15+
php_argv[3] = "internal_encoding=Windows-1252";
16+
php_argv[10] = "\xe2\x82\xac";
17+
}
18+
if (argc > 2 && strcmp(argv[2], "file") == 0) {
19+
php_argv[4] = "-f";
20+
php_argv[5] = "cli_argv_caf\xc3\xa9.php";
21+
} else if (argc > 2 && strcmp(argv[2], "invalid-utf8") == 0) {
22+
/* Latin-1 cafe with an accented e, which is not valid UTF-8. */
23+
php_argv[7] = "caf\xe9";
24+
}
25+
return do_php_cli((int) (sizeof(php_argv) / sizeof(*php_argv)) - 1, php_argv);
26+
}

‎sapi/embed/tests/cli_argv.phpt‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
do_php_cli() validates and converts custom UTF-8 argv on Windows
3+
--SKIPIF--
4+
<?php
5+
if (PHP_OS_FAMILY !== 'Windows') die('skip Windows only');
6+
if (!file_exists(dirname(PHP_BINARY) . '/php_embed_cli_test.exe')) {
7+
die('skip embed SAPI test host is not built (requires --enable-embed)');
8+
}
9+
?>
10+
--FILE--
11+
<?php
12+
$host = dirname(PHP_BINARY) . '/php_embed_cli_test.exe';
13+
foreach (['ignored-host-argument', 'default-charset', 'internal-encoding'] as $mode) {
14+
passthru(escapeshellarg($host) . ' ' . $mode, $status);
15+
var_dump($status);
16+
passthru(escapeshellarg($host) . ' ' . $mode . ' invalid-utf8 2>&1', $status);
17+
var_dump($status);
18+
}
19+
chdir(__DIR__);
20+
file_put_contents("cli_argv_caf\xc3\xa9.php", '<?php echo "Unicode filename works\n";');
21+
passthru(escapeshellarg($host) . ' default-charset file', $status);
22+
var_dump($status);
23+
?>
24+
--CLEAN--
25+
<?php
26+
unlink(__DIR__ . "/cli_argv_caf\xc3\xa9.php");
27+
?>
28+
--EXPECT--
29+
[5,["636166c3a9","617267756d656e74207769746820737061636573","","f09f9880"]]
30+
int(23)
31+
Invalid UTF-8 in command line argument 7.
32+
int(1)
33+
[5,["636166e9","617267756d656e74207769746820737061636573","","80"]]
34+
int(23)
35+
Invalid UTF-8 in command line argument 7.
36+
int(1)
37+
[5,["636166e9","617267756d656e74207769746820737061636573","","80"]]
38+
int(23)
39+
Invalid UTF-8 in command line argument 7.
40+
int(1)
41+
Unicode filename works
42+
int(0)

0 commit comments

Comments
 (0)