com php-src: Refactor php_escape_shell_* to return zend_string: ext/standard/basic_functions.c ext/sta ndard/exec.c ext/standard/exec.h
| From: | Dmitry Stogov | Date: | Mon, 03 Mar 2014 09:33:40 +0000 |
| Subject: | com php-src: Refactor php_escape_shell_* to return zend_string: ext/standard/basic_functions.c ext/sta ndard/exec.c ext/standard/exec.h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-77842@lists.php.net to get a copy of this message | ||
Commit: 70ddc853fd4757004ac488e6ee892897bb6f395a
Author: Xinchen Hui <laruence@gmail.com> Mon, 3 Mar 2014 17:32:56 +0800
Parents: d50782286f47e1cd50c2ff9ff9863b65d1e03af7
Branches: phpng
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=70ddc853fd4757004ac488e6ee892897bb6f395a
Log:
Refactor php_escape_shell_* to return zend_string
Changed paths:
M ext/standard/basic_functions.c
M ext/standard/exec.c
M ext/standard/exec.h
Diff:
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 76b45df..250b30d 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 fddf580..fb33812 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 ddb6367..8ffe471 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 */