diff options
Diffstat (limited to 'ext/standard/exec.c')
| -rw-r--r-- | ext/standard/exec.c | 80 |
1 files changed, 41 insertions, 39 deletions
diff --git a/ext/standard/exec.c b/ext/standard/exec.c index f8a22adf39..3b07e00443 100644 --- a/ext/standard/exec.c +++ b/ext/standard/exec.c @@ -120,7 +120,7 @@ PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value TSRMLS_ bufl = l + 1; buf[bufl] = '\0'; } - add_next_index_stringl(array, buf, bufl, 1); + add_next_index_stringl(array, buf, bufl); } b = buf; } @@ -134,12 +134,12 @@ PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value TSRMLS_ buf[bufl] = '\0'; } if (type == 2) { - add_next_index_stringl(array, buf, bufl, 1); + add_next_index_stringl(array, buf, bufl); } } /* Return last line from the shell command */ - RETVAL_STRINGL(buf, bufl, 1); + RETVAL_STRINGL(buf, bufl); } else { /* should return NULL, but for BC we return "" */ RETVAL_EMPTY_STRING(); } @@ -192,6 +192,7 @@ static void php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */ if (!ret_array) { ret = php_exec(mode, cmd, NULL, return_value TSRMLS_CC); } else { + ret_array = Z_REFVAL_P(ret_array); if (Z_TYPE_P(ret_array) != IS_ARRAY) { zval_dtor(ret_array); array_init(ret_array); @@ -199,6 +200,7 @@ static void php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */ ret = php_exec(2, cmd, ret_array, return_value TSRMLS_CC); } if (ret_code) { + ret_code = Z_REFVAL_P(ret_code); zval_dtor(ret_code); ZVAL_LONG(ret_code, ret); } @@ -238,16 +240,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 +258,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 +273,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 +307,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 +356,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 +366,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,8 +409,7 @@ PHP_FUNCTION(escapeshellcmd) } if (command_len) { - cmd = php_escape_shell_cmd(command); - RETVAL_STRING(cmd, 0); + RETVAL_STR(php_escape_shell_cmd(command)); } else { RETVAL_EMPTY_STRING(); } @@ -425,8 +429,7 @@ PHP_FUNCTION(escapeshellarg) } if (argument) { - cmd = php_escape_shell_arg(argument); - RETVAL_STRING(cmd, 0); + RETVAL_STR(php_escape_shell_arg(argument)); } } /* }}} */ @@ -436,10 +439,9 @@ PHP_FUNCTION(escapeshellarg) PHP_FUNCTION(shell_exec) { FILE *in; - size_t total_readbytes; char *command; int command_len; - char *ret; + zend_string *ret; php_stream *stream; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &command, &command_len) == FAILURE) { @@ -456,11 +458,11 @@ PHP_FUNCTION(shell_exec) } stream = php_stream_fopen_from_pipe(in, "rb"); - total_readbytes = php_stream_copy_to_mem(stream, &ret, PHP_STREAM_COPY_ALL, 0); + ret = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0); php_stream_close(stream); - if (total_readbytes > 0) { - RETVAL_STRINGL(ret, total_readbytes, 0); + if (ret && ret->len > 0) { + RETVAL_STR(ret); } } /* }}} */ |
