com php-src: Fixed bug #77632 (FFI Segfaults When Called With Variadics): ext/ffi/ffi.c ext/ffi/tests/bug77632.phpt
| From: | Dmitry Stogov | Date: | Mon, 18 Feb 2019 15:11:55 +0000 |
| Subject: | com php-src: Fixed bug #77632 (FFI Segfaults When Called With Variadics): ext/ffi/ffi.c ext/ffi/tests/bug77632.phpt | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-104697@lists.php.net to get a copy of this message | ||
Commit: 5661feb1ef929111ef7765672fcb49813eee86fc
Author: Dmitry Stogov <dmitry@zend.com> Mon, 18 Feb 2019 18:11:55 +0300
Parents: 2ed013c38aedcf9684d317d75951773cbb4fc275
Branches: PHP-7.4 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=5661feb1ef929111ef7765672fcb49813eee86fc
Log:
Fixed bug #77632 (FFI Segfaults When Called With Variadics)
Bugs:
https://bugs.php.net/77632
Changed paths:
M ext/ffi/ffi.c
A ext/ffi/tests/bug77632.phpt
Diff:
diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c
index 2da7fd4..5305a43 100644
--- a/ext/ffi/ffi.c
+++ b/ext/ffi/ffi.c
@@ -1776,14 +1776,12 @@ static int zend_ffi_cdata_get_closure(zval *obj, zend_class_entry **ce_ptr,
zend
func->common.arg_flags[2] = 0;
func->common.fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE;
func->common.function_name = ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE);
- func->common.num_args = func->common.required_num_args = type->func.args ?
zend_hash_num_elements(type->func.args) : 0;
+ /* set to 0 to avoid arg_info[] allocation, because all values are passed by value anyway */
+ func->common.num_args = 0;
+ func->common.required_num_args = type->func.args ?
zend_hash_num_elements(type->func.args) : 0;
+ func->common.arg_info = NULL;
func->internal_function.handler = ZEND_FN(ffi_trampoline);
- if (func->common.num_args > MAX_ARG_FLAG_NUM) {
- func->common.arg_info = emalloc(sizeof(zend_arg_info) * func->common.num_args);
- memset(func->common.arg_info, 0, sizeof(zend_arg_info) * func->common.num_args);
- }
-
func->internal_function.reserved[0] = type;
func->internal_function.reserved[1] = *(void**)cdata->ptr;
@@ -2447,9 +2445,6 @@ static ZEND_FUNCTION(ffi_trampoline) /* {{{ */
zend_string_release(EX(func)->common.function_name);
if (EX(func)->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
- if (EX(func)->common.arg_info) {
- efree(EX(func)->common.arg_info);
- }
zend_free_trampoline(EX(func));
EX(func) = NULL;
}
@@ -2507,14 +2502,12 @@ static zend_function *zend_ffi_get_func(zend_object **obj, zend_string
*name, co
func->common.arg_flags[2] = 0;
func->common.fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE;
func->common.function_name = zend_string_copy(name);
- func->common.num_args = func->common.required_num_args = type->func.args ?
zend_hash_num_elements(type->func.args) : 0;
+ /* set to 0 to avoid arg_info[] allocation, because all values are passed by value anyway */
+ func->common.num_args = 0;
+ func->common.required_num_args = type->func.args ?
zend_hash_num_elements(type->func.args) : 0;
+ func->common.arg_info = NULL;
func->internal_function.handler = ZEND_FN(ffi_trampoline);
- if (func->common.num_args > MAX_ARG_FLAG_NUM) {
- func->common.arg_info = emalloc(sizeof(zend_arg_info) * func->common.num_args);
- memset(func->common.arg_info, 0, sizeof(zend_arg_info) * func->common.num_args);
- }
-
func->internal_function.reserved[0] = type;
func->internal_function.reserved[1] = sym->addr;
diff --git a/ext/ffi/tests/bug77632.phpt b/ext/ffi/tests/bug77632.phpt
new file mode 100644
index 0000000..ebaf3dc
--- /dev/null
+++ b/ext/ffi/tests/bug77632.phpt
@@ -0,0 +1,14 @@
+--TEST--
+Bug #77632 (FFI Segfaults When Called With Variadics)
+--SKIPIF--
+<?php require_once('skipif.inc'); ?>
+--INI--
+ffi.enable=1
+--FILE--
+<?php
+$libc = FFI::cdef("int printf(const char *format, ...);", "libc.so.6");
+$args = ["test\n"];
+$libc->printf(...$args);
+?>
+--EXPECT--
+test