b7795d
From 71475b0af9677deeaf6fe55c0c5f53fec9f730d2 Mon Sep 17 00:00:00 2001
b7795d
From: Olly Betts <olly@survex.com>
b7795d
Date: Thu, 18 Mar 2021 15:50:52 +1300
b7795d
Subject: [PATCH] Improve PHP object creation
b7795d
b7795d
Reportedly the code we were using in the directorin case gave segfaults
b7795d
in PHP 7.2 and later - we've been unable to reproduce these, but the new
b7795d
approach is also simpler and should be bit faster too.
b7795d
b7795d
Fixes #1527, #1975
b7795d
---
b7795d
 CHANGES.current    |  6 ++++++
b7795d
 Lib/php/phprun.swg | 14 +++++---------
b7795d
 2 files changed, 11 insertions(+), 9 deletions(-)
b7795d
b7795d
#diff --git a/CHANGES.current b/CHANGES.current
b7795d
#index f287e3d60..79d41001f 100644
b7795d
#--- a/CHANGES.current
b7795d
#+++ b/CHANGES.current
b7795d
#@@ -7,6 +7,12 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
b7795d
# Version 4.1.0 (in progress)
b7795d
# ===========================
b7795d
# 
b7795d
#+2021-03-19: olly
b7795d
#+	    #1527 [PHP] Improve PHP object creation in directorin case.
b7795d
#+	    Reportedly the code we were using in this case gave segfaults in
b7795d
#+	    PHP 7.2 and later - we've been unable to reproduce these, but the
b7795d
#+	    new approach is also simpler and should be bit faster too.
b7795d
#+
b7795d
# 2021-03-18: olly
b7795d
# 	    #1655 [PHP] Fix char* typecheck typemap to accept PHP Null like the
b7795d
# 	    corresponding in typemap does.
b7795d
diff --git a/Lib/php/phprun.swg b/Lib/php/phprun.swg
b7795d
index a07a1b9f8..f3a4e6ad1 100644
b7795d
--- a/Lib/php/phprun.swg
b7795d
+++ b/Lib/php/phprun.swg
b7795d
@@ -90,15 +90,13 @@ SWIG_SetPointerZval(zval *z, void *ptr, swig_type_info *type, int newobject) {
b7795d
     } else {
b7795d
       /*
b7795d
        * Wrap the resource in an object, the resource will be accessible
b7795d
-       * via the "_cPtr" member. This is currently only used by
b7795d
+       * via the "_cPtr" property. This code path is currently only used by
b7795d
        * directorin typemaps.
b7795d
        */
b7795d
-      zval resource;
b7795d
       zend_class_entry *ce = NULL;
b7795d
       const char *type_name = type->name+3; /* +3 so: _p_Foo -> Foo */
b7795d
       size_t type_name_len;
b7795d
       const char * p;
b7795d
-      HashTable * ht;
b7795d
 
b7795d
       /* Namespace__Foo -> Foo */
b7795d
       /* FIXME: ugly and goes wrong for classes with __ in their names. */
b7795d
@@ -107,7 +105,6 @@ SWIG_SetPointerZval(zval *z, void *ptr, swig_type_info *type, int newobject) {
b7795d
       }
b7795d
       type_name_len = strlen(type_name);
b7795d
 
b7795d
-      ZVAL_RES(&resource, zend_register_resource(value, *(int *)(type->clientdata)));
b7795d
       if (SWIG_PREFIX_LEN > 0) {
b7795d
         zend_string * classname = zend_string_alloc(SWIG_PREFIX_LEN + type_name_len, 0);
b7795d
         memcpy(classname->val, SWIG_PREFIX, SWIG_PREFIX_LEN);
b7795d
@@ -121,13 +118,12 @@ SWIG_SetPointerZval(zval *z, void *ptr, swig_type_info *type, int newobject) {
b7795d
       }
b7795d
       if (ce == NULL) {
b7795d
         /* class does not exist */
b7795d
-        ce = zend_standard_class_def;
b7795d
+        object_init(z);
b7795d
+      } else {
b7795d
+        object_init_ex(z, ce);
b7795d
       }
b7795d
 
b7795d
-      ALLOC_HASHTABLE(ht);
b7795d
-      zend_hash_init(ht, 1, NULL, NULL, 0);
b7795d
-      zend_hash_str_update(ht, "_cPtr", sizeof("_cPtr") - 1, &resource);
b7795d
-      object_and_properties_init(z, ce, ht);
b7795d
+      add_property_resource_ex(z, "_cPtr", sizeof("_cPtr") - 1, zend_register_resource(value, *(int *)(type->clientdata)));
b7795d
     }
b7795d
     return;
b7795d
   }
b7795d
-- 
b7795d
2.26.3
b7795d