From ed7f151b31986ef1f0e70a5880b40191901bde3f Mon Sep 17 00:00:00 2001 From: Bogdan Padalko Date: Tue, 2 May 2017 21:50:13 +0300 Subject: [PATCH] Fix segfault when zero args passed to V8\FunctionObject::NewInstance() --- src/php_v8_function.cc | 4 +- tests/V8FunctionObject_NewInstance.phpt | 41 +++++++++++++++++++ ...ctTemplate_SetHandlerForNamedProperty.phpt | 2 +- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 tests/V8FunctionObject_NewInstance.phpt diff --git a/src/php_v8_function.cc b/src/php_v8_function.cc index 9c43d0e..9835e68 100644 --- a/src/php_v8_function.cc +++ b/src/php_v8_function.cc @@ -345,7 +345,7 @@ static PHP_METHOD(V8Function, __construct) { static PHP_METHOD(V8Function, NewInstance) { zval *php_v8_context_zv; - zval* arguments_zv; + zval *arguments_zv = NULL; int argc = 0; v8::Local *argv = NULL; @@ -388,7 +388,7 @@ static PHP_METHOD(V8Function, NewInstance) { static PHP_METHOD(V8Function, Call) { zval *php_v8_context_zv; - zval *php_v8_recv_zv = NULL; + zval *php_v8_recv_zv; zval *arguments_zv = NULL; int argc = 0; diff --git a/tests/V8FunctionObject_NewInstance.phpt b/tests/V8FunctionObject_NewInstance.phpt new file mode 100644 index 0000000..baec4ae --- /dev/null +++ b/tests/V8FunctionObject_NewInstance.phpt @@ -0,0 +1,41 @@ +--TEST-- +V8\FunctionObject::NewInstance() +--SKIPIF-- + +--FILE-- +GlobalObject(); + + +$tpl = new \V8\FunctionTemplate($isolate, function (\V8\FunctionCallbackInfo $args) { + echo 'called as ', $args->IsConstructCall() ? 'constructor' : 'function', ' '; + echo 'with ', count($args->Arguments()), ' arguments'; + + echo PHP_EOL; +}); + + +$tpl->GetFunction($context)->NewInstance($context); +$tpl->GetFunction($context)->NewInstance($context, [new \V8\StringValue($isolate, 'argument1')]); +$tpl->GetFunction($context)->NewInstance($context, [new \V8\ObjectValue($context)]); + + +?> +--EXPECT-- +called as constructor with 0 arguments +called as constructor with 1 arguments +called as constructor with 1 arguments diff --git a/tests/V8ObjectTemplate_SetHandlerForNamedProperty.phpt b/tests/V8ObjectTemplate_SetHandlerForNamedProperty.phpt index 24f9848..0d1d360 100644 --- a/tests/V8ObjectTemplate_SetHandlerForNamedProperty.phpt +++ b/tests/V8ObjectTemplate_SetHandlerForNamedProperty.phpt @@ -27,7 +27,7 @@ $getter = function (\V8\NameValue $name, \V8\PropertyCallbackInfo $info) use (&$ $info->GetReturnValue()->Set(new \V8\NumberValue($info->GetIsolate(), $foo)); }; -$setter = function (\V8\NameValue$name, \V8\Value $value, \V8\PropertyCallbackInfo $info) use (&$foo) { +$setter = function (\V8\NameValue $name, \V8\Value $value, \V8\PropertyCallbackInfo $info) use (&$foo) { echo 'I am named setter for ', $name->ToString($info->GetContext())->Value(), '!', PHP_EOL; $foo = $value->ToNumber($info->GetContext())->Value() / 2;