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