Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MFH: Added zend_eval_stringl and made create_function(), etc. binary-…

…safe
  • Loading branch information...
commit d55300298e0e3ec0bdbb58ff3dcfda113d21852e 1 parent a80be68
Matt Wilmas authored
View
29 Zend/zend_builtin_functions.c
@@ -1673,7 +1673,7 @@ ZEND_FUNCTION(get_defined_vars)
ZEND_FUNCTION(create_function)
{
char *eval_code, *function_name, *function_args, *function_code;
- int function_name_length, function_args_len, function_code_len;
+ int eval_code_length, function_name_length, function_args_len, function_code_len;
int retval;
char *eval_name;
@@ -1681,10 +1681,29 @@ ZEND_FUNCTION(create_function)
return;
}
- zend_spprintf(&eval_code, 0, "function " LAMBDA_TEMP_FUNCNAME "(%s){%s}", function_args, function_code);
+ eval_code = (char *) emalloc(sizeof("function " LAMBDA_TEMP_FUNCNAME)
+ +function_args_len
+ +2 /* for the args parentheses */
+ +2 /* for the curly braces */
+ +function_code_len);
+
+ eval_code_length = sizeof("function " LAMBDA_TEMP_FUNCNAME "(") - 1;
+ memcpy(eval_code, "function " LAMBDA_TEMP_FUNCNAME "(", eval_code_length);
+
+ memcpy(eval_code + eval_code_length, function_args, function_args_len);
+ eval_code_length += function_args_len;
+
+ eval_code[eval_code_length++] = ')';
+ eval_code[eval_code_length++] = '{';
+
+ memcpy(eval_code + eval_code_length, function_code, function_code_len);
+ eval_code_length += function_code_len;
+
+ eval_code[eval_code_length++] = '}';
+ eval_code[eval_code_length] = '\0';
eval_name = zend_make_compiled_string_description("runtime-created function" TSRMLS_CC);
- retval = zend_eval_string(eval_code, NULL, eval_name TSRMLS_CC);
+ retval = zend_eval_stringl(eval_code, eval_code_length, NULL, eval_name TSRMLS_CC);
efree(eval_code);
efree(eval_name);
@@ -1699,10 +1718,10 @@ ZEND_FUNCTION(create_function)
function_add_ref(&new_function);
function_name = (char *) emalloc(sizeof("0lambda_")+MAX_LENGTH_OF_LONG);
+ function_name[0] = '\0';
do {
- sprintf(function_name, "%clambda_%d", 0, ++EG(lambda_count));
- function_name_length = strlen(function_name+1)+1;
+ function_name_length = 1 + sprintf(function_name + 1, "lambda_%d", ++EG(lambda_count));
} while (zend_hash_add(EG(function_table), function_name, function_name_length+1, &new_function, sizeof(zend_function), NULL)==FAILURE);
zend_hash_del(EG(function_table), LAMBDA_TEMP_FUNCNAME, sizeof(LAMBDA_TEMP_FUNCNAME));
RETURN_STRINGL(function_name, function_name_length, 0);
View
2  Zend/zend_execute.h
@@ -73,7 +73,9 @@ static inline void safe_free_zval_ptr_rel(zval *p ZEND_FILE_LINE_DC ZEND_FILE_LI
ZEND_API int zend_lookup_class(const char *name, int name_length, zend_class_entry ***ce TSRMLS_DC);
ZEND_API int zend_lookup_class_ex(const char *name, int name_length, int use_autoload, zend_class_entry ***ce TSRMLS_DC);
ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC);
+ZEND_API int zend_eval_stringl(char *str, int str_len, zval *retval_ptr, char *string_name TSRMLS_DC);
ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC);
+ZEND_API int zend_eval_stringl_ex(char *str, int str_len, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC);
static inline int i_zend_is_true(zval *op)
{
View
25 Zend/zend_execute_API.c
@@ -1116,7 +1116,7 @@ ZEND_API int zend_lookup_class(const char *name, int name_length, zend_class_ent
}
/* }}} */
-ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC) /* {{{ */
+ZEND_API int zend_eval_stringl(char *str, int str_len, zval *retval_ptr, char *string_name TSRMLS_DC) /* {{{ */
{
zval pv;
zend_op_array *new_op_array;
@@ -1125,15 +1125,14 @@ ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSR
int retval;
if (retval_ptr) {
- int l = strlen(str);
- Z_STRLEN(pv) = l + sizeof("return ;") - 1;
+ Z_STRLEN(pv) = str_len + sizeof("return ;") - 1;
Z_STRVAL(pv) = emalloc(Z_STRLEN(pv) + 1);
memcpy(Z_STRVAL(pv), "return ", sizeof("return ") - 1);
- memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, str, l);
+ memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, str, str_len);
Z_STRVAL(pv)[Z_STRLEN(pv) - 1] = ';';
Z_STRVAL(pv)[Z_STRLEN(pv)] = '\0';
} else {
- Z_STRLEN(pv) = strlen(str);
+ Z_STRLEN(pv) = str_len;
Z_STRVAL(pv) = str;
}
Z_TYPE(pv) = IS_STRING;
@@ -1188,11 +1187,17 @@ ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSR
}
/* }}} */
-ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC) /* {{{ */
+ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC) /* {{{ */
+{
+ return zend_eval_stringl(str, strlen(str), retval_ptr, string_name TSRMLS_CC);
+}
+/* }}} */
+
+ZEND_API int zend_eval_stringl_ex(char *str, int str_len, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC) /* {{{ */
{
int result;
- result = zend_eval_string(str, retval_ptr, string_name TSRMLS_CC);
+ result = zend_eval_stringl(str, str_len, retval_ptr, string_name TSRMLS_CC);
if (handle_exceptions && EG(exception)) {
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
result = FAILURE;
@@ -1201,6 +1206,12 @@ ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name,
}
/* }}} */
+ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC) /* {{{ */
+{
+ return zend_eval_stringl_ex(str, strlen(str), retval_ptr, string_name, handle_exceptions TSRMLS_CC);
+}
+/* }}} */
+
void execute_new_code(TSRMLS_D) /* {{{ */
{
zend_op *opline, *end;
View
2  ext/interbase/php_ibase_udf.c
@@ -165,7 +165,7 @@ void exec_php(BLOBCALLBACK b, PARAMDSC *res, ISC_SHORT *init)
#endif
/* feed it to the parser */
zend_first_try {
- result = zend_eval_string(code, NULL, "Firebird Embedded PHP engine" TSRMLS_CC);
+ result = zend_eval_stringl(code, b->blob_total_length, NULL, "Firebird Embedded PHP engine" TSRMLS_CC);
} zend_end_try();
}
View
4 ext/mbstring/php_mbregex.c
@@ -914,9 +914,9 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
if (eval) {
zval v;
/* null terminate buffer */
- smart_str_appendc(&eval_buf, '\0');
+ smart_str_0(&eval_buf);
/* do eval */
- if (zend_eval_string(eval_buf.c, &v, description TSRMLS_CC) == FAILURE) {
+ if (zend_eval_stringl(eval_buf.c, eval_buf.len, &v, description TSRMLS_CC) == FAILURE) {
efree(description);
php_error_docref(NULL TSRMLS_CC,E_ERROR, "Failed evaluating code: %s%s", PHP_EOL, eval_buf.c);
/* zend_error() does not return in this case */
View
2  ext/pcre/php_pcre.c
@@ -913,7 +913,7 @@ static int preg_do_eval(char *eval_str, int eval_str_len, char *subject,
compiled_string_description = zend_make_compiled_string_description("regexp code" TSRMLS_CC);
/* Run the code */
- if (zend_eval_string(code.c, &retval, compiled_string_description TSRMLS_CC) == FAILURE) {
+ if (zend_eval_stringl(code.c, code.len, &retval, compiled_string_description TSRMLS_CC) == FAILURE) {
efree(compiled_string_description);
php_error_docref(NULL TSRMLS_CC,E_ERROR, "Failed evaluating code: %s%s", PHP_EOL, code.c);
/* zend_error() does not return in this case */
View
2  ext/standard/assert.c
@@ -164,7 +164,7 @@ PHP_FUNCTION(assert)
}
compiled_string_description = zend_make_compiled_string_description("assert code" TSRMLS_CC);
- if (zend_eval_string(myeval, &retval, compiled_string_description TSRMLS_CC) == FAILURE) {
+ if (zend_eval_stringl(myeval, Z_STRLEN_PP(assertion), &retval, compiled_string_description TSRMLS_CC) == FAILURE) {
efree(compiled_string_description);
php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s", PHP_EOL, myeval);
if (ASSERTG(bail)) {
View
2  sapi/cli/php_cli.c
@@ -1165,7 +1165,7 @@ int main(int argc, char *argv[])
continue;
}
- zend_eval_string(code, NULL, "php shell code" TSRMLS_CC);
+ zend_eval_stringl(code, pos, NULL, "php shell code" TSRMLS_CC);
pos = 0;
if (php_last_char != '\0' && php_last_char != '\n') {
Please sign in to comment.
Something went wrong with that request. Please try again.