Skip to content

Commit 2ac633f

Browse files
committed
Fix some memory leaks
1 parent e4c14a1 commit 2ac633f

File tree

4 files changed

+66
-55
lines changed

4 files changed

+66
-55
lines changed

ext/soap/php_http.c

+1
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ int make_http_soap_request(zval *this_ptr,
477477
use_ssl = 1;
478478
} else if (phpurl->scheme == NULL || !zend_string_equals_literal(phpurl->scheme, "http")) {
479479
php_url_free(phpurl);
480+
php_uri_free(uri_internal);
480481
if (request != buf) {
481482
zend_string_release_ex(request, 0);
482483
}

ext/uri/php_uri.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,16 @@ PHP_METHOD(Uri_Rfc3986Uri, normalize)
422422

423423
zend_object *new_object = uri_clone_obj_handler(this_object);
424424
if (UNEXPECTED(EG(exception) != NULL)) {
425+
zend_object_release(new_object);
425426
RETURN_THROWS();
426427
}
428+
427429
uri_internal_t *new_internal_uri = uri_internal_from_obj(new_object);
428-
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, this_object);
430+
URI_CHECK_INITIALIZATION_RETURN_THROWS(new_internal_uri, new_object); /* TODO fix memory leak of new_object */
429431

430432
if (UNEXPECTED(internal_uri->handler->normalize_uri(new_internal_uri->uri) == FAILURE)) {
431433
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(this_object->ce->name));
434+
zend_object_release(new_object);
432435
RETURN_THROWS();
433436
}
434437

ext/uri/php_uri_common.h

+58-51
Original file line numberDiff line numberDiff line change
@@ -91,91 +91,98 @@ uri_property_handler_t *uri_property_handler_from_internal_uri(const uri_interna
9191
void throw_invalid_uri_exception(zval *errors);
9292

9393
#define URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, object) do { \
94-
ZEND_ASSERT(internal_uri != NULL); \
94+
ZEND_ASSERT(internal_uri != NULL); \
9595
if (UNEXPECTED(internal_uri->uri == NULL)) { \
96-
zend_throw_error(uninitialized_uri_exception_ce, "%s object is not correctly initialized", ZSTR_VAL(object->ce->name)); \
97-
RETURN_THROWS(); \
98-
} \
96+
zend_throw_error(uninitialized_uri_exception_ce, "%s object is not correctly initialized", ZSTR_VAL(object->ce->name)); \
97+
RETURN_THROWS(); \
98+
} \
9999
} while (0)
100100

101101
#define URI_CHECK_INITIALIZATION_RETURN(internal_uri, object, return_on_failure) do { \
102-
ZEND_ASSERT(internal_uri != NULL); \
102+
ZEND_ASSERT(internal_uri != NULL); \
103103
if (UNEXPECTED(internal_uri->uri == NULL)) { \
104-
zend_throw_error(uninitialized_uri_exception_ce, "%s object is not correctly initialized", ZSTR_VAL(object->ce->name)); \
105-
return return_on_failure; \
104+
zend_throw_error(uninitialized_uri_exception_ce, "%s object is not correctly initialized", ZSTR_VAL(object->ce->name)); \
105+
return return_on_failure; \
106106
} \
107107
} while (0)
108108

109109
#define URI_CHECK_INITIALIZATION_RETURN_VOID(internal_uri, object) do { \
110-
ZEND_ASSERT(internal_uri != NULL); \
110+
ZEND_ASSERT(internal_uri != NULL); \
111111
if (UNEXPECTED(internal_uri->uri == NULL)) { \
112-
zend_throw_error(uninitialized_uri_exception_ce, "%s object is not correctly initialized", ZSTR_VAL(object->ce->name)); \
113-
return; \
112+
zend_throw_error(uninitialized_uri_exception_ce, "%s object is not correctly initialized", ZSTR_VAL(object->ce->name)); \
113+
return; \
114114
} \
115115
} while (0)
116116

117117
#define URI_GETTER(property_name) do { \
118-
ZEND_PARSE_PARAMETERS_NONE(); \
119-
uri_internal_t *internal_uri = Z_URI_INTERNAL_P(ZEND_THIS); \
120-
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, Z_OBJ_P(ZEND_THIS)); \
121-
const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name); \
122-
ZEND_ASSERT(property_handler != NULL); \
123-
if (property_handler->read_func(internal_uri, return_value) == FAILURE) { \
124-
zend_throw_error(NULL, "%s::$%s property cannot be retrieved", ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name), ZSTR_VAL(property_name)); \
125-
RETURN_THROWS(); \
126-
} \
118+
ZEND_PARSE_PARAMETERS_NONE(); \
119+
uri_internal_t *internal_uri = Z_URI_INTERNAL_P(ZEND_THIS); \
120+
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, Z_OBJ_P(ZEND_THIS)); \
121+
const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name); \
122+
ZEND_ASSERT(property_handler != NULL); \
123+
if (UNEXPECTED(property_handler->read_func(internal_uri, return_value) == FAILURE)) { \
124+
zend_throw_error(NULL, "%s::$%s property cannot be retrieved", ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name), ZSTR_VAL(property_name)); \
125+
RETURN_THROWS(); \
126+
} \
127127
} while (0)
128128

129129
#define URI_WITHER_COMMON(property_name, property_zv, return_value) \
130130
uri_internal_t *internal_uri = Z_URI_INTERNAL_P(ZEND_THIS); \
131-
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, Z_OBJ_P(ZEND_THIS)); \
132-
const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name); \
133-
ZEND_ASSERT(property_handler != NULL); \
134-
zend_object *new_object = uri_clone_obj_handler(Z_OBJ_P(ZEND_THIS)); \
131+
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, Z_OBJ_P(ZEND_THIS)); \
132+
const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name); \
133+
ZEND_ASSERT(property_handler != NULL); \
134+
zend_object *new_object = uri_clone_obj_handler(Z_OBJ_P(ZEND_THIS)); \
135135
if (UNEXPECTED(EG(exception) != NULL)) { \
136+
zend_object_release(new_object); \
137+
zval_ptr_dtor(property_zv); \
136138
RETURN_THROWS(); \
137139
} \
138-
uri_internal_t *new_internal_uri = uri_internal_from_obj(new_object); \
139-
URI_CHECK_INITIALIZATION_RETURN_THROWS(new_internal_uri, Z_OBJ_P(ZEND_THIS)); \
140-
if (property_handler->write_func == NULL) { \
141-
zend_readonly_property_modification_error_ex(ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name), ZSTR_VAL(property_name)); \
142-
RETURN_THROWS(); \
143-
} \
144-
zval errors; \
145-
ZVAL_UNDEF(&errors); \
140+
uri_internal_t *new_internal_uri = uri_internal_from_obj(new_object); \
141+
URI_CHECK_INITIALIZATION_RETURN_THROWS(new_internal_uri, Z_OBJ_P(ZEND_THIS)); /* TODO fix memory leak of new_object */ \
142+
if (property_handler->write_func == NULL) { \
143+
zend_readonly_property_modification_error_ex(ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name), ZSTR_VAL(property_name)); \
144+
zend_object_release(new_object); \
145+
zval_ptr_dtor(property_zv); \
146+
RETURN_THROWS(); \
147+
} \
148+
zval errors; \
149+
ZVAL_UNDEF(&errors); \
146150
if (property_handler->write_func(new_internal_uri, property_zv, &errors) == FAILURE) { \
147151
throw_invalid_uri_exception(&errors); \
148152
zval_ptr_dtor(&errors); \
149-
RETURN_THROWS(); \
150-
} \
151-
ZEND_ASSERT(Z_TYPE(errors) == IS_UNDEF); \
152-
ZVAL_OBJ(return_value, new_object);
153+
zend_object_release(new_object); \
154+
zval_ptr_dtor(property_zv); \
155+
RETURN_THROWS(); \
156+
} \
157+
ZEND_ASSERT(Z_TYPE(errors) == IS_UNDEF); \
158+
ZVAL_OBJ(return_value, new_object); \
159+
zval_ptr_dtor(property_zv);
153160

154161
#define URI_WITHER_STR(property_name) do { \
155-
zend_string *value; \
156-
ZEND_PARSE_PARAMETERS_START(1, 1) \
157-
Z_PARAM_PATH_STR_OR_NULL(value) \
158-
ZEND_PARSE_PARAMETERS_END(); \
162+
zend_string *value; \
163+
ZEND_PARSE_PARAMETERS_START(1, 1) \
164+
Z_PARAM_PATH_STR_OR_NULL(value) \
165+
ZEND_PARSE_PARAMETERS_END(); \
159166
zval zv; \
160-
if (value == NULL) { \
161-
ZVAL_NULL(&zv); \
167+
if (value == NULL) { \
168+
ZVAL_NULL(&zv); \
162169
} else { \
163-
ZVAL_STR_COPY(&zv, value); \
164-
} \
170+
ZVAL_STR_COPY(&zv, value); \
171+
} \
165172
URI_WITHER_COMMON(property_name, &zv, return_value) \
166173
} while (0)
167174

168175
#define URI_WITHER_LONG(property_name) do { \
169-
zend_long value; \
170-
bool value_is_null; \
171-
ZEND_PARSE_PARAMETERS_START(1, 1) \
172-
Z_PARAM_LONG_OR_NULL(value, value_is_null) \
173-
ZEND_PARSE_PARAMETERS_END(); \
176+
zend_long value; \
177+
bool value_is_null; \
178+
ZEND_PARSE_PARAMETERS_START(1, 1) \
179+
Z_PARAM_LONG_OR_NULL(value, value_is_null) \
180+
ZEND_PARSE_PARAMETERS_END(); \
174181
zval zv; \
175-
if (value_is_null) {\
182+
if (value_is_null) {\
176183
ZVAL_NULL(&zv); \
177-
} else { \
178-
ZVAL_LONG(&zv, value); \
184+
} else { \
185+
ZVAL_LONG(&zv, value); \
179186
} \
180187
URI_WITHER_COMMON(property_name, &zv, return_value); \
181188
} while (0)

ext/uri/php_uriparser.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ typedef enum {
5757
UriUriA *new_uriparser_uri = uriparser_parse_uri(str, NULL, errors); \
5858
if (new_uriparser_uri == NULL) { \
5959
smart_str_free(&uri_str); \
60-
zend_string_free(str); \
60+
zend_string_release(str); \
6161
return FAILURE; \
6262
} \
6363
uriparser_free_uri(uriparser_uri); \
6464
internal_uri->uri = (void *) new_uriparser_uri; \
65-
smart_str_free(&uri_str); \
65+
smart_str_free(&uri_str); \
6666
return SUCCESS; \
6767
} while (0)
6868

@@ -119,7 +119,7 @@ static void uriparser_append_scheme(const uri_internal_t *internal_uri, smart_st
119119
uriparser_read_scheme(internal_uri, &tmp);
120120
if (Z_TYPE(tmp) == IS_STRING) {
121121
smart_str_append(uri_str, Z_STR(tmp));
122-
smart_str_appendl(uri_str, "://", sizeof("://") - 1); // TODO apply the algorithm at https://datatracker.ietf.org/doc/html/rfc3986#section-5.3
122+
smart_str_appendl(uri_str, "://", sizeof("://") - 1); /* TODO apply the algorithm at https://datatracker.ietf.org/doc/html/rfc3986#section-5.3 */
123123
}
124124

125125
zval_ptr_dtor(&tmp);

0 commit comments

Comments
 (0)