Refactor php_escape_shell_* to return zend_string

This commit is contained in:
Xinchen Hui 2014-03-03 17:32:56 +08:00
parent d50782286f
commit 70ddc853fd
3 changed files with 35 additions and 36 deletions

View file

@ -723,7 +723,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_call_user_method, 0, 0, 2) ZEND_BEGIN_ARG_INFO_EX(arginfo_call_user_method, 0, 0, 2)
ZEND_ARG_INFO(0, method_name) 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, parameter)
ZEND_ARG_INFO(0, ...) ZEND_ARG_INFO(0, ...)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()

View file

@ -238,16 +238,16 @@ PHP_FUNCTION(passthru)
*NOT* safe for binary strings *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); register int x, y, l = strlen(str);
char *cmd;
char *p = NULL; char *p = NULL;
size_t estimate = (2 * l) + 1; size_t estimate = (2 * l) + 1;
zend_string *cmd;
TSRMLS_FETCH(); TSRMLS_FETCH();
cmd = safe_emalloc(2, l, 1); cmd = STR_ALLOC(2 * l, 0);
for (x = 0, y = 0; x < l; x++) { for (x = 0, y = 0; x < l; x++) {
int mb_len = php_mblen(str + 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) { if (mb_len < 0) {
continue; continue;
} else if (mb_len > 1) { } else if (mb_len > 1) {
memcpy(cmd + y, str + x, mb_len); memcpy(cmd->val + y, str + x, mb_len);
y += mb_len; y += mb_len;
x += mb_len - 1; x += mb_len - 1;
continue; continue;
@ -271,13 +271,13 @@ PHPAPI char *php_escape_shell_cmd(char *str)
} else if (p && *p == str[x]) { } else if (p && *p == str[x]) {
p = NULL; p = NULL;
} else { } else {
cmd[y++] = '\\'; cmd->val[y++] = '\\';
} }
cmd[y++] = str[x]; cmd->val[y++] = str[x];
break; break;
#else #else
/* % is Windows specific for enviromental variables, ^%PATH% will /* % 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 '%':
case '"': case '"':
@ -305,44 +305,46 @@ PHPAPI char *php_escape_shell_cmd(char *str)
case '\x0A': /* excluding these two */ case '\x0A': /* excluding these two */
case '\xFF': case '\xFF':
#ifdef PHP_WIN32 #ifdef PHP_WIN32
cmd[y++] = '^'; cmd->val[y++] = '^';
#else #else
cmd[y++] = '\\'; cmd->val[y++] = '\\';
#endif #endif
/* fall-through */ /* fall-through */
default: default:
cmd[y++] = str[x]; cmd->val[y++] = str[x];
} }
} }
cmd[y] = '\0'; cmd->val[y] = '\0';
if ((estimate - y) > 4096) { if ((estimate - y) > 4096) {
/* realloc if the estimate was way overill /* realloc if the estimate was way overill
* Arbitrary cutoff point of 4096 */ * Arbitrary cutoff point of 4096 */
cmd = erealloc(cmd, y + 1); cmd = STR_REALLOC(cmd, y, 0);
} }
cmd->len = y;
return cmd; return cmd;
} }
/* }}} */ /* }}} */
/* {{{ php_escape_shell_arg /* {{{ 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); int x, y = 0, l = strlen(str);
char *cmd; zend_string *cmd;
size_t estimate = (4 * l) + 3; size_t estimate = (4 * l) + 3;
TSRMLS_FETCH(); TSRMLS_FETCH();
cmd = safe_emalloc(4, l, 3); /* worst case */ cmd = STR_ALLOC(4 * l + 2, 0); /* worst case */
#ifdef PHP_WIN32 #ifdef PHP_WIN32
cmd[y++] = '"'; cmd->val[y++] = '"';
#else #else
cmd[y++] = '\''; cmd->val[y++] = '\'';
#endif #endif
for (x = 0; x < l; x++) { for (x = 0; x < l; x++) {
@ -352,7 +354,7 @@ PHPAPI char *php_escape_shell_arg(char *str)
if (mb_len < 0) { if (mb_len < 0) {
continue; continue;
} else if (mb_len > 1) { } else if (mb_len > 1) {
memcpy(cmd + y, str + x, mb_len); memcpy(cmd->val + y, str + x, mb_len);
y += mb_len; y += mb_len;
x += mb_len - 1; x += mb_len - 1;
continue; continue;
@ -362,31 +364,32 @@ PHPAPI char *php_escape_shell_arg(char *str)
#ifdef PHP_WIN32 #ifdef PHP_WIN32
case '"': case '"':
case '%': case '%':
cmd[y++] = ' '; cmd->val[y++] = ' ';
break; break;
#else #else
case '\'': case '\'':
cmd[y++] = '\''; cmd->val[y++] = '\'';
cmd[y++] = '\\'; cmd->val[y++] = '\\';
cmd[y++] = '\''; cmd->val[y++] = '\'';
#endif #endif
/* fall-through */ /* fall-through */
default: default:
cmd[y++] = str[x]; cmd->val[y++] = str[x];
} }
} }
#ifdef PHP_WIN32 #ifdef PHP_WIN32
cmd[y++] = '"'; cmd->val[y++] = '"';
#else #else
cmd[y++] = '\''; cmd->val[y++] = '\'';
#endif #endif
cmd[y] = '\0'; cmd->val[y] = '\0';
if ((estimate - y) > 4096) { if ((estimate - y) > 4096) {
/* realloc if the estimate was way overill /* realloc if the estimate was way overill
* Arbitrary cutoff point of 4096 */ * Arbitrary cutoff point of 4096 */
cmd = erealloc(cmd, y + 1); cmd = STR_REALLOC(cmd, y, 0);
} }
cmd->len = y;
return cmd; return cmd;
} }
/* }}} */ /* }}} */
@ -404,9 +407,7 @@ PHP_FUNCTION(escapeshellcmd)
} }
if (command_len) { if (command_len) {
cmd = php_escape_shell_cmd(command); RETVAL_STRING(php_escape_shell_cmd(command));
//??? RETVAL_STRING(cmd, 0);
RETVAL_STRING(cmd);
} else { } else {
RETVAL_EMPTY_STRING(); RETVAL_EMPTY_STRING();
} }
@ -426,9 +427,7 @@ PHP_FUNCTION(escapeshellarg)
} }
if (argument) { if (argument) {
cmd = php_escape_shell_arg(argument); RETVAL_STR(php_escape_shell_arg(argument));
//??? RETVAL_STRING(cmd, 0);
RETVAL_STRING(cmd);
} }
} }
/* }}} */ /* }}} */

View file

@ -34,8 +34,8 @@ PHP_FUNCTION(proc_terminate);
PHP_FUNCTION(proc_nice); PHP_FUNCTION(proc_nice);
PHP_MINIT_FUNCTION(proc_open); PHP_MINIT_FUNCTION(proc_open);
PHPAPI char *php_escape_shell_cmd(char *); PHPAPI zend_string *php_escape_shell_cmd(char *);
PHPAPI char *php_escape_shell_arg(char *); PHPAPI zend_string *php_escape_shell_arg(char *);
PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value TSRMLS_DC); PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value TSRMLS_DC);
#endif /* EXEC_H */ #endif /* EXEC_H */