Skip to content

Commit

Permalink
Fix some memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Nov 13, 2024
1 parent e4c14a1 commit d894dad
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 55 deletions.
1 change: 1 addition & 0 deletions ext/soap/php_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ int make_http_soap_request(zval *this_ptr,
use_ssl = 1;
} else if (phpurl->scheme == NULL || !zend_string_equals_literal(phpurl->scheme, "http")) {
php_url_free(phpurl);
php_uri_free(uri_internal);
if (request != buf) {
zend_string_release_ex(request, 0);
}
Expand Down
5 changes: 4 additions & 1 deletion ext/uri/php_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,16 @@ PHP_METHOD(Uri_Rfc3986Uri, normalize)

zend_object *new_object = uri_clone_obj_handler(this_object);
if (UNEXPECTED(EG(exception) != NULL)) {
zend_object_release(new_object);
RETURN_THROWS();
}

uri_internal_t *new_internal_uri = uri_internal_from_obj(new_object);
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, this_object);
URI_CHECK_INITIALIZATION_RETURN_THROWS(new_internal_uri, new_object); /* TODO fix memory leak of new_object */

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

Expand Down
109 changes: 58 additions & 51 deletions ext/uri/php_uri_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,91 +91,98 @@ uri_property_handler_t *uri_property_handler_from_internal_uri(const uri_interna
void throw_invalid_uri_exception(zval *errors);

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

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

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

#define URI_GETTER(property_name) do { \
ZEND_PARSE_PARAMETERS_NONE(); \
uri_internal_t *internal_uri = Z_URI_INTERNAL_P(ZEND_THIS); \
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, Z_OBJ_P(ZEND_THIS)); \
const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name); \
ZEND_ASSERT(property_handler != NULL); \
if (property_handler->read_func(internal_uri, return_value) == FAILURE) { \
zend_throw_error(NULL, "%s::$%s property cannot be retrieved", ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name), ZSTR_VAL(property_name)); \
RETURN_THROWS(); \
} \
ZEND_PARSE_PARAMETERS_NONE(); \
uri_internal_t *internal_uri = Z_URI_INTERNAL_P(ZEND_THIS); \
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, Z_OBJ_P(ZEND_THIS)); \
const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name); \
ZEND_ASSERT(property_handler != NULL); \
if (UNEXPECTED(property_handler->read_func(internal_uri, return_value) == FAILURE)) { \
zend_throw_error(NULL, "%s::$%s property cannot be retrieved", ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name), ZSTR_VAL(property_name)); \
RETURN_THROWS(); \
} \
} while (0)

#define URI_WITHER_COMMON(property_name, property_zv, return_value) \
uri_internal_t *internal_uri = Z_URI_INTERNAL_P(ZEND_THIS); \
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, Z_OBJ_P(ZEND_THIS)); \
const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name); \
ZEND_ASSERT(property_handler != NULL); \
zend_object *new_object = uri_clone_obj_handler(Z_OBJ_P(ZEND_THIS)); \
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, Z_OBJ_P(ZEND_THIS)); \
const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name); \
ZEND_ASSERT(property_handler != NULL); \
zend_object *new_object = uri_clone_obj_handler(Z_OBJ_P(ZEND_THIS)); \
if (UNEXPECTED(EG(exception) != NULL)) { \
zend_object_release(new_object); \
zval_ptr_dtor(property_zv); \
RETURN_THROWS(); \
} \
uri_internal_t *new_internal_uri = uri_internal_from_obj(new_object); \
URI_CHECK_INITIALIZATION_RETURN_THROWS(new_internal_uri, Z_OBJ_P(ZEND_THIS)); \
if (property_handler->write_func == NULL) { \
zend_readonly_property_modification_error_ex(ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name), ZSTR_VAL(property_name)); \
RETURN_THROWS(); \
} \
zval errors; \
ZVAL_UNDEF(&errors); \
uri_internal_t *new_internal_uri = uri_internal_from_obj(new_object); \
URI_CHECK_INITIALIZATION_RETURN_THROWS(new_internal_uri, Z_OBJ_P(ZEND_THIS)); /* TODO fix memory leak of new_object */ \
if (property_handler->write_func == NULL) { \
zend_readonly_property_modification_error_ex(ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name), ZSTR_VAL(property_name)); \
zend_object_release(new_object); \
zval_ptr_dtor(property_zv); \
RETURN_THROWS(); \
} \
zval errors; \
ZVAL_UNDEF(&errors); \
if (property_handler->write_func(new_internal_uri, property_zv, &errors) == FAILURE) { \
throw_invalid_uri_exception(&errors); \
zval_ptr_dtor(&errors); \
RETURN_THROWS(); \
} \
ZEND_ASSERT(Z_TYPE(errors) == IS_UNDEF); \
ZVAL_OBJ(return_value, new_object);
zend_object_release(new_object); \
zval_ptr_dtor(property_zv); \
RETURN_THROWS(); \
} \
ZEND_ASSERT(Z_TYPE(errors) == IS_UNDEF); \
ZVAL_OBJ(return_value, new_object); \
zval_ptr_dtor(property_zv);

#define URI_WITHER_STR(property_name) do { \
zend_string *value; \
ZEND_PARSE_PARAMETERS_START(1, 1) \
Z_PARAM_PATH_STR_OR_NULL(value) \
ZEND_PARSE_PARAMETERS_END(); \
zend_string *value; \
ZEND_PARSE_PARAMETERS_START(1, 1) \
Z_PARAM_PATH_STR_OR_NULL(value) \
ZEND_PARSE_PARAMETERS_END(); \
zval zv; \
if (value == NULL) { \
ZVAL_NULL(&zv); \
if (value == NULL) { \
ZVAL_NULL(&zv); \
} else { \
ZVAL_STR_COPY(&zv, value); \
} \
ZVAL_STR_COPY(&zv, value); \
} \
URI_WITHER_COMMON(property_name, &zv, return_value) \
} while (0)

#define URI_WITHER_LONG(property_name) do { \
zend_long value; \
bool value_is_null; \
ZEND_PARSE_PARAMETERS_START(1, 1) \
Z_PARAM_LONG_OR_NULL(value, value_is_null) \
ZEND_PARSE_PARAMETERS_END(); \
zend_long value; \
bool value_is_null; \
ZEND_PARSE_PARAMETERS_START(1, 1) \
Z_PARAM_LONG_OR_NULL(value, value_is_null) \
ZEND_PARSE_PARAMETERS_END(); \
zval zv; \
if (value_is_null) {\
if (value_is_null) {\
ZVAL_NULL(&zv); \
} else { \
ZVAL_LONG(&zv, value); \
} else { \
ZVAL_LONG(&zv, value); \
} \
URI_WITHER_COMMON(property_name, &zv, return_value); \
} while (0)
Expand Down
6 changes: 3 additions & 3 deletions ext/uri/php_uriparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ typedef enum {
UriUriA *new_uriparser_uri = uriparser_parse_uri(str, NULL, errors); \
if (new_uriparser_uri == NULL) { \
smart_str_free(&uri_str); \
zend_string_free(str); \
zend_string_release(str); \
return FAILURE; \
} \
uriparser_free_uri(uriparser_uri); \
internal_uri->uri = (void *) new_uriparser_uri; \
smart_str_free(&uri_str); \
smart_str_free(&uri_str); \
return SUCCESS; \
} while (0)

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

zval_ptr_dtor(&tmp);
Expand Down

0 comments on commit d894dad

Please sign in to comment.