From 6139024663154e9d02f5418621f7ee7f20e00c0d Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sun, 18 Jan 2015 10:37:27 +0100 Subject: [PATCH] fix leaks / invalid frees --- php_http.c | 1 + php_http_client.c | 2 +- php_http_client_curl.c | 6 +++--- php_http_message.c | 2 +- php_http_object.c | 18 +++++++++++++++++- php_http_object.h | 2 ++ php_http_options.c | 7 ++----- php_http_options.h | 1 + 8 files changed, 28 insertions(+), 11 deletions(-) diff --git a/php_http.c b/php_http.c index 0bf09ac..7a28933 100644 --- a/php_http.c +++ b/php_http.c @@ -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) diff --git a/php_http_client.c b/php_http_client.c index 3ee3ff5..9672575 100644 --- a/php_http_client.c +++ b/php_http_client.c @@ -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) diff --git a/php_http_client_curl.c b/php_http_client_curl.c index da19346..6753e57 100644 --- a/php_http_client_curl.c +++ b/php_http_client_curl.c @@ -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; diff --git a/php_http_message.c b/php_http_message.c index 5d4b3ed..fdfc3d6 100644 --- a/php_http_message.c +++ b/php_http_message.c @@ -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); diff --git a/php_http_object.c b/php_http_object.c index d2ff649..9024ee9 100644 --- a/php_http_object.c +++ b/php_http_object.c @@ -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 diff --git a/php_http_object.h b/php_http_object.h index 7bb34f1..24e5a18 100644 --- a/php_http_object.h +++ b/php_http_object.h @@ -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 diff --git a/php_http_options.c b/php_http_options.c index 7a01b9c..d4be512 100644 --- a/php_http_options.c +++ b/php_http_options.c @@ -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; diff --git a/php_http_options.h b/php_http_options.h index f685f8e..4fff611 100644 --- a/php_http_options.h +++ b/php_http_options.h @@ -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); -- 2.30.2