From 70ddc853fd4757004ac488e6ee892897bb6f395a Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Mon, 3 Mar 2014 17:32:56 +0800 Subject: [PATCH] Refactor php_escape_shell_* to return zend_string --- ext/standard/basic_functions.c | 2 +- ext/standard/exec.c | 65 +++++++++++++++++----------------- ext/standard/exec.h | 4 +-- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 76b45df18e..250b30d9d2 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -723,7 +723,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_call_user_method, 0, 0, 2) ZEND_ARG_INFO(0, method_name) - ZEND_ARG_INFO(1, object) + ZEND_ARG_INFO(0, object) ZEND_ARG_INFO(0, parameter) ZEND_ARG_INFO(0, ...) ZEND_END_ARG_INFO() diff --git a/ext/standard/exec.c b/ext/standard/exec.c index fddf580508..fb33812c19 100644 --- a/ext/standard/exec.c +++ b/ext/standard/exec.c @@ -238,16 +238,16 @@ PHP_FUNCTION(passthru) *NOT* safe for binary strings */ -PHPAPI char *php_escape_shell_cmd(char *str) +PHPAPI zend_string *php_escape_shell_cmd(char *str) { register int x, y, l = strlen(str); - char *cmd; char *p = NULL; size_t estimate = (2 * l) + 1; + zend_string *cmd; TSRMLS_FETCH(); - cmd = safe_emalloc(2, l, 1); + cmd = STR_ALLOC(2 * l, 0); for (x = 0, y = 0; x < l; x++) { int mb_len = php_mblen(str + x, (l - x)); @@ -256,7 +256,7 @@ PHPAPI char *php_escape_shell_cmd(char *str) if (mb_len < 0) { continue; } else if (mb_len > 1) { - memcpy(cmd + y, str + x, mb_len); + memcpy(cmd->val + y, str + x, mb_len); y += mb_len; x += mb_len - 1; continue; @@ -271,13 +271,13 @@ PHPAPI char *php_escape_shell_cmd(char *str) } else if (p && *p == str[x]) { p = NULL; } else { - cmd[y++] = '\\'; + cmd->val[y++] = '\\'; } - cmd[y++] = str[x]; + cmd->val[y++] = str[x]; break; #else /* % is Windows specific for enviromental variables, ^%PATH% will - output PATH whil ^%PATH^% not. escapeshellcmd will escape all %. + output PATH whil ^%PATH^% not. escapeshellcmd->val will escape all %. */ case '%': case '"': @@ -305,44 +305,46 @@ PHPAPI char *php_escape_shell_cmd(char *str) case '\x0A': /* excluding these two */ case '\xFF': #ifdef PHP_WIN32 - cmd[y++] = '^'; + cmd->val[y++] = '^'; #else - cmd[y++] = '\\'; + cmd->val[y++] = '\\'; #endif /* fall-through */ default: - cmd[y++] = str[x]; + cmd->val[y++] = str[x]; } } - cmd[y] = '\0'; + cmd->val[y] = '\0'; if ((estimate - y) > 4096) { /* realloc if the estimate was way overill * Arbitrary cutoff point of 4096 */ - cmd = erealloc(cmd, y + 1); + cmd = STR_REALLOC(cmd, y, 0); } + cmd->len = y; + return cmd; } /* }}} */ /* {{{ php_escape_shell_arg */ -PHPAPI char *php_escape_shell_arg(char *str) +PHPAPI zend_string *php_escape_shell_arg(char *str) { int x, y = 0, l = strlen(str); - char *cmd; + zend_string *cmd; size_t estimate = (4 * l) + 3; TSRMLS_FETCH(); - cmd = safe_emalloc(4, l, 3); /* worst case */ + cmd = STR_ALLOC(4 * l + 2, 0); /* worst case */ #ifdef PHP_WIN32 - cmd[y++] = '"'; + cmd->val[y++] = '"'; #else - cmd[y++] = '\''; + cmd->val[y++] = '\''; #endif for (x = 0; x < l; x++) { @@ -352,7 +354,7 @@ PHPAPI char *php_escape_shell_arg(char *str) if (mb_len < 0) { continue; } else if (mb_len > 1) { - memcpy(cmd + y, str + x, mb_len); + memcpy(cmd->val + y, str + x, mb_len); y += mb_len; x += mb_len - 1; continue; @@ -362,31 +364,32 @@ PHPAPI char *php_escape_shell_arg(char *str) #ifdef PHP_WIN32 case '"': case '%': - cmd[y++] = ' '; + cmd->val[y++] = ' '; break; #else case '\'': - cmd[y++] = '\''; - cmd[y++] = '\\'; - cmd[y++] = '\''; + cmd->val[y++] = '\''; + cmd->val[y++] = '\\'; + cmd->val[y++] = '\''; #endif /* fall-through */ default: - cmd[y++] = str[x]; + cmd->val[y++] = str[x]; } } #ifdef PHP_WIN32 - cmd[y++] = '"'; + cmd->val[y++] = '"'; #else - cmd[y++] = '\''; + cmd->val[y++] = '\''; #endif - cmd[y] = '\0'; + cmd->val[y] = '\0'; if ((estimate - y) > 4096) { /* realloc if the estimate was way overill * Arbitrary cutoff point of 4096 */ - cmd = erealloc(cmd, y + 1); + cmd = STR_REALLOC(cmd, y, 0); } + cmd->len = y; return cmd; } /* }}} */ @@ -404,9 +407,7 @@ PHP_FUNCTION(escapeshellcmd) } if (command_len) { - cmd = php_escape_shell_cmd(command); -//??? RETVAL_STRING(cmd, 0); - RETVAL_STRING(cmd); + RETVAL_STRING(php_escape_shell_cmd(command)); } else { RETVAL_EMPTY_STRING(); } @@ -426,9 +427,7 @@ PHP_FUNCTION(escapeshellarg) } if (argument) { - cmd = php_escape_shell_arg(argument); -//??? RETVAL_STRING(cmd, 0); - RETVAL_STRING(cmd); + RETVAL_STR(php_escape_shell_arg(argument)); } } /* }}} */ diff --git a/ext/standard/exec.h b/ext/standard/exec.h index ddb6367570..8ffe471408 100644 --- a/ext/standard/exec.h +++ b/ext/standard/exec.h @@ -34,8 +34,8 @@ PHP_FUNCTION(proc_terminate); PHP_FUNCTION(proc_nice); PHP_MINIT_FUNCTION(proc_open); -PHPAPI char *php_escape_shell_cmd(char *); -PHPAPI char *php_escape_shell_arg(char *); +PHPAPI zend_string *php_escape_shell_cmd(char *); +PHPAPI zend_string *php_escape_shell_arg(char *); PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value TSRMLS_DC); #endif /* EXEC_H */ -- 2.50.1