fix leaks / invalid frees
authorMichael Wallner <mike@php.net>
Sun, 18 Jan 2015 09:37:27 +0000 (10:37 +0100)
committerMichael Wallner <mike@php.net>
Sun, 18 Jan 2015 09:37:27 +0000 (10:37 +0100)
php_http.c
php_http_client.c
php_http_client_curl.c
php_http_message.c
php_http_object.c
php_http_object.h
php_http_options.c
php_http_options.h

index 0bf09ac775a44c39584e5ef5faae926a67c31ece..7a28933b03e37be7f6f99ba6c1690992d7caab35 100644 (file)
@@ -128,6 +128,7 @@ PHP_MINIT_FUNCTION(http)
        REGISTER_INI_ENTRIES();
        
        if (0
+       || SUCCESS != PHP_MINIT_CALL(http_object)
        || SUCCESS != PHP_MINIT_CALL(http_exception)
        || SUCCESS != PHP_MINIT_CALL(http_cookie)
        || SUCCESS != PHP_MINIT_CALL(http_encoding)
index 3ee3ff523d62509d9f88bd1f40f9047da92649c7..9672575fcc4f76a5316b686efb84d97de025d72e 100644 (file)
@@ -22,7 +22,7 @@ static HashTable php_http_client_drivers;
 
 static void php_http_client_driver_hash_dtor(zval *pData)
 {
-       efree(Z_PTR_P(pData));
+       pefree(Z_PTR_P(pData), 1);
 }
 
 ZEND_RESULT_CODE php_http_client_driver_add(php_http_client_driver_t *driver)
index da19346b70168390cec95e54ea17f744d6409c11..6753e57cf8eb337db1d49739c162fa89fa69c920 100644 (file)
@@ -1227,7 +1227,7 @@ static void php_http_curle_options_init(php_http_options_t *registry)
        /* useragent */
        if ((opt = php_http_option_register(registry, ZEND_STRL("useragent"), CURLOPT_USERAGENT, IS_STRING))) {
                /* don't check strlen, to allow sending no useragent at all */
-               ZVAL_STRING(&opt->defval,
+               ZVAL_PSTRING(&opt->defval,
                                "PECL_HTTP/" PHP_PECL_HTTP_VERSION " "
                                "PHP/" PHP_VERSION " "
                                "libcurl/" LIBCURL_VERSION);
@@ -1319,7 +1319,7 @@ static void php_http_curle_options_init(php_http_options_t *registry)
                }
                if ((opt = php_http_option_register(registry, ZEND_STRL("certtype"), CURLOPT_SSLCERTTYPE, IS_STRING))) {
                        opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
-                       ZVAL_STRING(&opt->defval, "PEM");
+                       ZVAL_PSTRING(&opt->defval, "PEM");
                }
                if ((opt = php_http_option_register(registry, ZEND_STRL("key"), CURLOPT_SSLKEY, IS_STRING))) {
                        opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
@@ -1327,7 +1327,7 @@ static void php_http_curle_options_init(php_http_options_t *registry)
                }
                if ((opt = php_http_option_register(registry, ZEND_STRL("keytype"), CURLOPT_SSLKEYTYPE, IS_STRING))) {
                        opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
-                       ZVAL_STRING(&opt->defval, "PEM");
+                       ZVAL_PSTRING(&opt->defval, "PEM");
                }
                if ((opt = php_http_option_register(registry, ZEND_STRL("keypasswd"), CURLOPT_SSLKEYPASSWD, IS_STRING))) {
                        opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
index 5d4b3edba8495ab64bd6223f2acac2742917fe04..fdfc3d640594d85e7c454ffdba1af8ac676e957d 100644 (file)
@@ -500,7 +500,7 @@ static HashTable php_http_message_object_prophandlers;
 
 static void php_http_message_object_prophandler_hash_dtor(zval *pData)
 {
-       efree(Z_PTR_P(pData));
+       pefree(Z_PTR_P(pData), 1);
 }
 
 typedef void (*php_http_message_object_prophandler_func_t)(php_http_message_object_t *o, zval *v);
index d2ff6493149fdc0387d0a7413f3f468efbe5a25b..9024ee9a47b53a4d757cead7afc3c4c765d48a4b 100644 (file)
@@ -12,6 +12,8 @@
 
 #include "php_http_api.h"
 
+static zend_object_handlers php_http_object_handlers;
+
 zend_object *php_http_object_new(zend_class_entry *ce)
 {
        return &php_http_object_new_ex(ce, NULL)->zo;
@@ -26,11 +28,17 @@ php_http_object_t *php_http_object_new_ex(zend_class_entry *ce, void *intern)
        object_properties_init(&o->zo, ce);
 
        o->intern = intern;
-       o->zo.handlers = zend_get_std_object_handlers();
+       o->zo.handlers = &php_http_object_handlers;
 
        return o;
 }
 
+void php_http_object_free(zend_object *object)
+{
+       php_http_object_t *obj = PHP_HTTP_OBJ(object, NULL);
+       zend_object_std_dtor(object);
+}
+
 ZEND_RESULT_CODE php_http_new(void **obj_ptr, zend_class_entry *ce, php_http_new_t create, zend_class_entry *parent_ce, void *intern_ptr)
 {
        void *obj;
@@ -75,6 +83,14 @@ ZEND_RESULT_CODE php_http_method_call(zval *object, const char *method_str, size
        return rv;
 }
 
+PHP_MINIT_FUNCTION(http_object)
+{
+       memcpy(&php_http_object_handlers, zend_get_std_object_handlers(), sizeof(php_http_object_handlers));
+       php_http_object_handlers.offset = XtOffsetOf(php_http_object_t, zo);
+
+       return SUCCESS;
+}
+
 /*
  * Local variables:
  * tab-width: 4
index 7bb34f16f757b99ab250cfb6950364083516f1f2..24e5a181c568983344ca2952447d2e5073e3087b 100644 (file)
@@ -26,6 +26,8 @@ typedef void *(*php_http_new_t)(zend_class_entry *ce, void *);
 ZEND_RESULT_CODE php_http_new(void **obj_ptr, zend_class_entry *ce, php_http_new_t create, zend_class_entry *parent_ce, void *intern_ptr);
 ZEND_RESULT_CODE php_http_method_call(zval *object, const char *method_str, size_t method_len, int argc, zval argv[], zval *retval_ptr);
 
+PHP_MINIT_FUNCTION(http_object);
+
 #endif
 
 
index 7a01b9cc385586fe5d7570af136db402d12be796..d4be512a7e1e9584cdace1a12429054fee90e471 100644 (file)
@@ -19,7 +19,7 @@ static void php_http_options_hash_dtor(zval *pData)
        zval_ptr_dtor(&opt->defval);
        zend_hash_destroy(&opt->suboptions.options);
        zend_string_release(opt->name);
-       efree(opt);
+       pefree(opt, opt->persistent);
 }
 
 php_http_options_t *php_http_options_init(php_http_options_t *registry, zend_bool persistent)
@@ -84,6 +84,7 @@ php_http_option_t *php_http_option_register(php_http_options_t *registry, const
        opt.suboptions.getter = registry->getter;
        opt.suboptions.setter = registry->setter;
 
+       opt.persistent = registry->persistent;
        opt.name = zend_string_init(name_str, name_len, registry->persistent);
        opt.type = type;
        opt.option = option;
@@ -101,10 +102,6 @@ php_http_option_t *php_http_option_register(php_http_options_t *registry, const
                ZVAL_LONG(&opt.defval, 0);
                break;
 
-       case IS_STRING:
-               ZVAL_EMPTY_STRING(&opt.defval);
-               break;
-
        case IS_DOUBLE:
                ZVAL_DOUBLE(&opt.defval, 0);
                break;
index f685f8e6902f51f2910579c8d833737afa72e4f2..4fff611a3b40aadfeac1c5d210894b369a9ec0dd 100644 (file)
@@ -38,6 +38,7 @@ struct php_http_option {
        zval defval;
 
        php_http_option_set_callback_t setter;
+       unsigned persistent:1;
 };
 
 PHP_HTTP_API php_http_options_t *php_http_options_init(php_http_options_t *registry, zend_bool persistent);