Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions ext/uri/php_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -1089,9 +1089,9 @@ PHP_RINIT_FUNCTION(uri)
return SUCCESS;
}

PHP_RSHUTDOWN_FUNCTION(uri)
ZEND_MODULE_POST_ZEND_DEACTIVATE_D(uri)
{
if (PHP_RSHUTDOWN(uri_parser_whatwg)(INIT_FUNC_ARGS_PASSTHRU) == FAILURE) {
if (ZEND_MODULE_POST_ZEND_DEACTIVATE_N(uri_parser_whatwg)() == FAILURE) {
return FAILURE;
}

Expand All @@ -1106,8 +1106,10 @@ zend_module_entry uri_module_entry = {
PHP_MINIT(uri), /* PHP_MINIT - Module initialization */
PHP_MSHUTDOWN(uri), /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(uri), /* PHP_RINIT - Request initialization */
PHP_RSHUTDOWN(uri), /* PHP_RSHUTDOWN - Request shutdown */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(uri), /* PHP_MINFO - Module info */
PHP_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES
NO_MODULE_GLOBALS,
ZEND_MODULE_POST_ZEND_DEACTIVATE_N(uri),
STANDARD_MODULE_PROPERTIES_EX
};
23 changes: 23 additions & 0 deletions ext/uri/tests/056.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Test Lexbor memory management
--EXTENSIONS--
uri
--FILE--
<?php

$urls = [];

for ($i = 0; $i < 1000; $i++) {
$urls[] = Uri\WhatWg\Url::parse("https://example.com/{$i}/");
}

for ($i = 0; $i < 1000; $i++) {
if ($urls[$i]->toAsciiString() !== "https://example.com/{$i}/") {
die("FAIL");
}
}

?>
==DONE==
--EXPECT--
==DONE==
23 changes: 5 additions & 18 deletions ext/uri/uri_parser_whatwg.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
ZEND_TLS lexbor_mraw_t lexbor_mraw = {0};
ZEND_TLS lxb_url_parser_t lexbor_parser = {0};
ZEND_TLS lxb_unicode_idna_t lexbor_idna = {0};
ZEND_TLS unsigned short int parsed_urls;

static const unsigned short int maximum_parses_before_cleanup = 500;
static const size_t lexbor_mraw_byte_size = 8192;

static zend_always_inline void zval_string_or_null_to_lexbor_str(zval *value, lexbor_str_t *lexbor_str)
Expand Down Expand Up @@ -530,8 +528,6 @@ PHP_RINIT_FUNCTION(uri_parser_whatwg)
goto fail;
}

parsed_urls = 0;

return SUCCESS;

fail:
Expand All @@ -548,7 +544,7 @@ PHP_RINIT_FUNCTION(uri_parser_whatwg)
return FAILURE;
}

PHP_RSHUTDOWN_FUNCTION(uri_parser_whatwg)
ZEND_MODULE_POST_ZEND_DEACTIVATE_D(uri_parser_whatwg)
{
lxb_unicode_idna_destroy(&lexbor_idna, false);
memset(&lexbor_idna, 0, sizeof(lexbor_idna));
Expand All @@ -557,24 +553,12 @@ PHP_RSHUTDOWN_FUNCTION(uri_parser_whatwg)
lexbor_mraw_destroy(&lexbor_mraw, false);
memset(&lexbor_mraw, 0, sizeof(lexbor_mraw));

parsed_urls = 0;

return SUCCESS;
}

static void reset_parser_state(void)
{
if (++parsed_urls % maximum_parses_before_cleanup == 0) {
lexbor_mraw_clean(lexbor_parser.mraw);
parsed_urls = 0;
}

lxb_url_parser_clean(&lexbor_parser);
}

lxb_url_t *php_uri_parser_whatwg_parse_ex(const char *uri_str, size_t uri_str_len, const lxb_url_t *lexbor_base_url, zval *errors, bool silent)
{
reset_parser_state();
lxb_url_parser_clean(&lexbor_parser);

lxb_url_t *url = lxb_url_parse(&lexbor_parser, lexbor_base_url, (unsigned char *) uri_str, uri_str_len);
const char *reason = fill_errors(errors);
Expand Down Expand Up @@ -624,6 +608,9 @@ static zend_string *php_uri_parser_whatwg_to_string(void *uri, uri_recomposition

static void php_uri_parser_whatwg_free(void *uri)
{
lxb_url_t *lexbor_uri = uri;

lxb_url_destroy(lexbor_uri);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't even need the separate variable, but doesn't really matter

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware. I considered it good practice to always turn the void* into the correctly typed pointer at the first opportunity as an assistance to the human reader.

}

const uri_parser_t php_uri_parser_whatwg = {
Expand Down
2 changes: 1 addition & 1 deletion ext/uri/uri_parser_whatwg.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ extern const uri_parser_t php_uri_parser_whatwg;
lxb_url_t *php_uri_parser_whatwg_parse_ex(const char *uri_str, size_t uri_str_len, const lxb_url_t *lexbor_base_url, zval *errors, bool silent);

PHP_RINIT_FUNCTION(uri_parser_whatwg);
PHP_RSHUTDOWN_FUNCTION(uri_parser_whatwg);
ZEND_MODULE_POST_ZEND_DEACTIVATE_D(uri_parser_whatwg);

#endif