Skip to content

Commit 85cc2b3

Browse files
committed
ext/standard: Make php_escape_shell_arg() take a zend_string* instead of char*
This saves on an expensive strlen() computation
1 parent db6ba01 commit 85cc2b3

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

UPGRADING.INTERNALS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ PHP 8.4 INTERNALS UPGRADE NOTES
249249
- The php_escape_shell_cmd() now takes a zend_string* instead of a char*
250250
Moreover, providing it with a binary safe string is the responsibility of
251251
the caller now.
252+
- The php_escape_shell_arg() now takes a zend_string* instead of a char*
253+
Moreover, providing it with a binary safe string is the responsibility of
254+
the caller now.
252255

253256
========================
254257
4. OpCode changes

ext/standard/exec.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,15 @@ PHPAPI zend_string *php_escape_shell_cmd(const zend_string *unescaped_cmd)
389389
/* }}} */
390390

391391
/* {{{ php_escape_shell_arg */
392-
PHPAPI zend_string *php_escape_shell_arg(const char *str)
392+
PHPAPI zend_string *php_escape_shell_arg(const zend_string *unescaped_arg)
393393
{
394394
size_t x, y = 0;
395-
size_t l = strlen(str);
396395
zend_string *cmd;
396+
397+
ZEND_ASSERT(ZSTR_LEN(unescaped_arg) == strlen(ZSTR_VAL(unescaped_arg)) && "Must be a binary safe string");
398+
size_t l = ZSTR_LEN(unescaped_arg);
399+
const char *str = ZSTR_VAL(unescaped_arg);
400+
397401
uint64_t estimate = (4 * (uint64_t)l) + 3;
398402

399403
/* max command line length - two single quotes - \0 byte length */
@@ -492,18 +496,12 @@ PHP_FUNCTION(escapeshellcmd)
492496
/* {{{ Quote and escape an argument for use in a shell command */
493497
PHP_FUNCTION(escapeshellarg)
494498
{
495-
char *argument;
496-
size_t argument_len;
499+
zend_string *argument;
497500

498501
ZEND_PARSE_PARAMETERS_START(1, 1)
499-
Z_PARAM_STRING(argument, argument_len)
502+
Z_PARAM_PATH_STR(argument)
500503
ZEND_PARSE_PARAMETERS_END();
501504

502-
if (argument_len != strlen(argument)) {
503-
zend_argument_value_error(1, "must not contain any null bytes");
504-
RETURN_THROWS();
505-
}
506-
507505
RETVAL_STR(php_escape_shell_arg(argument));
508506
}
509507
/* }}} */

ext/standard/exec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PHP_MINIT_FUNCTION(proc_open);
2121
PHP_MINIT_FUNCTION(exec);
2222

2323
PHPAPI zend_string *php_escape_shell_cmd(const zend_string *unescaped_cmd);
24-
PHPAPI zend_string *php_escape_shell_arg(const char *str);
24+
PHPAPI zend_string *php_escape_shell_arg(const zend_string *unescaped_arg);
2525
PHPAPI int php_exec(int type, const char *cmd, zval *array, zval *return_value);
2626

2727
#endif /* EXEC_H */

0 commit comments

Comments
 (0)