Improve PHP object creation

Reportedly the code we were using in the directorin case gave segfaults
in PHP 7.2 and later - we've been unable to reproduce these, but the new
approach is also simpler and should be bit faster too.

Fixes #1527, #1975
This commit is contained in:
Olly Betts 2021-03-18 15:50:52 +13:00
commit 71475b0af9
2 changed files with 11 additions and 9 deletions

View file

@ -7,6 +7,12 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.1.0 (in progress)
===========================
2021-03-19: olly
#1527 [PHP] Improve PHP object creation in directorin case.
Reportedly the code we were using in this case gave segfaults in
PHP 7.2 and later - we've been unable to reproduce these, but the
new approach is also simpler and should be bit faster too.
2021-03-18: olly
#1655 [PHP] Fix char* typecheck typemap to accept PHP Null like the
corresponding in typemap does.

View file

@ -90,15 +90,13 @@ SWIG_SetPointerZval(zval *z, void *ptr, swig_type_info *type, int newobject) {
} else {
/*
* Wrap the resource in an object, the resource will be accessible
* via the "_cPtr" member. This is currently only used by
* via the "_cPtr" property. This code path is currently only used by
* directorin typemaps.
*/
zval resource;
zend_class_entry *ce = NULL;
const char *type_name = type->name+3; /* +3 so: _p_Foo -> Foo */
size_t type_name_len;
const char * p;
HashTable * ht;
/* Namespace__Foo -> Foo */
/* FIXME: ugly and goes wrong for classes with __ in their names. */
@ -107,7 +105,6 @@ SWIG_SetPointerZval(zval *z, void *ptr, swig_type_info *type, int newobject) {
}
type_name_len = strlen(type_name);
ZVAL_RES(&resource, zend_register_resource(value, *(int *)(type->clientdata)));
if (SWIG_PREFIX_LEN > 0) {
zend_string * classname = zend_string_alloc(SWIG_PREFIX_LEN + type_name_len, 0);
memcpy(classname->val, SWIG_PREFIX, SWIG_PREFIX_LEN);
@ -121,13 +118,12 @@ SWIG_SetPointerZval(zval *z, void *ptr, swig_type_info *type, int newobject) {
}
if (ce == NULL) {
/* class does not exist */
ce = zend_standard_class_def;
object_init(z);
} else {
object_init_ex(z, ce);
}
ALLOC_HASHTABLE(ht);
zend_hash_init(ht, 1, NULL, NULL, 0);
zend_hash_str_update(ht, "_cPtr", sizeof("_cPtr") - 1, &resource);
object_and_properties_init(z, ce, ht);
add_property_resource_ex(z, "_cPtr", sizeof("_cPtr") - 1, zend_register_resource(value, *(int *)(type->clientdata)));
}
return;
}