Fix c extension doesn' allow message reference in array (#5599)

* Fix c extension doesn' allow message reference in array

* Fix array constructor handling reference of array.

* Change test name
This commit is contained in:
Paul Yang 2019-01-22 14:02:44 -08:00 committed by GitHub
parent a4c09f3eba
commit 1069565a68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 0 deletions

View File

@ -319,6 +319,11 @@ void Message_construct(zval* msg, zval* array_wrapper) {
zend_hash_move_forward_ex(array, &pointer)) {
zend_hash_get_current_key_zval_ex(array, &key, &pointer);
field = upb_msgdef_ntofz(intern->descriptor->msgdef, Z_STRVAL_P(&key));
#if PHP_MAJOR_VERSION >= 7
if (Z_ISREF_P((CACHED_VALUE*)value)) {
value = Z_REFVAL_P((CACHED_VALUE*)value);
}
#endif
if (field == NULL) {
zend_error(E_USER_ERROR, "Unknown field: %s", Z_STRVAL_P(&key));
}

View File

@ -186,6 +186,12 @@ bool native_slot_set_by_array(upb_fieldtype_t type,
break;
}
case UPB_TYPE_MESSAGE: {
#if PHP_MAJOR_VERSION >= 7
if (Z_ISREF_P(value)) {
ZVAL_DEREF(value);
}
#endif
if (Z_TYPE_P(value) != IS_OBJECT) {
zend_error(E_USER_ERROR, "Given value is not message.");
return false;

View File

@ -529,6 +529,37 @@ class RepeatedFieldTest extends \PHPUnit\Framework\TestCase
$this->assertSame(3, $arr[2]);
}
#########################################################
# Test reference in array
#########################################################
public function testArrayElementIsReference()
{
$m = new TestMessage();
$subs = [1, 2];
foreach ($subs as &$sub) {
$sub = new Sub(['a' => $sub]);
}
$m->setRepeatedMessage($subs);
}
public function testArrayIsReference()
{
$keys = [['repeated_message' => [['a' => 1]]]];
foreach ($keys as &$key) {
foreach ($key['repeated_message'] as &$element) {
$element = new Sub($element);
}
$key = new TestMessage($key);
}
$m = new TestMessage();
$m->setRepeatedDeep($keys);
}
#########################################################
# Test memory leak
#########################################################

View File

@ -31,6 +31,7 @@ message TestMessage {
Sub optional_message = 17;
bar.TestInclude optional_included_message = 18;
TestMessage recursive = 19;
repeated TestMessage repeated_deep = 20;
// Repeated
repeated int32 repeated_int32 = 31;