Skip to content

Commit 2260bdc

Browse files
committed
Don't allocate the lexbor parser on the heap
1 parent 820a0e1 commit 2260bdc

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

ext/uri/php_lexbor.c

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <arpa/inet.h>
2424
#endif
2525

26-
ZEND_TLS lxb_url_parser_t *lexbor_parser;
26+
ZEND_TLS lxb_url_parser_t lexbor_parser;
2727
ZEND_TLS int lexbor_urls;
2828

2929
#define LEXBOR_MAX_URL_COUNT 500
@@ -45,7 +45,7 @@ static zend_always_inline void zval_long_or_null_to_lexbor_str(zval *value, lexb
4545
{
4646
if (Z_TYPE_P(value) == IS_LONG) {
4747
ZVAL_STR(value, zend_long_to_str(Z_LVAL_P(value)));
48-
lexbor_str_init_append(lexbor_str, lexbor_parser->mraw, (const lxb_char_t *) Z_STRVAL_P(value), Z_STRLEN_P(value));
48+
lexbor_str_init_append(lexbor_str, lexbor_parser.mraw, (const lxb_char_t *) Z_STRVAL_P(value), Z_STRLEN_P(value));
4949
zval_ptr_dtor_str(value);
5050
} else {
5151
ZEND_ASSERT(Z_ISNULL_P(value));
@@ -57,10 +57,10 @@ static zend_always_inline void zval_long_or_null_to_lexbor_str(zval *value, lexb
5757
static void lexbor_cleanup_parser(void)
5858
{
5959
if (++lexbor_urls % LEXBOR_MAX_URL_COUNT == 0) {
60-
lexbor_mraw_clean(lexbor_parser->mraw);
60+
lexbor_mraw_clean(lexbor_parser.mraw);
6161
}
6262

63-
lxb_url_parser_clean(lexbor_parser);
63+
lxb_url_parser_clean(&lexbor_parser);
6464
}
6565

6666
/**
@@ -81,12 +81,12 @@ static void fill_errors(zval *errors)
8181

8282
array_init(errors);
8383

84-
if (lexbor_parser->log == NULL) {
84+
if (lexbor_parser.log == NULL) {
8585
return;
8686
}
8787

8888
lexbor_plog_entry_t *lxb_error;
89-
while ((lxb_error = lexbor_array_obj_pop(&lexbor_parser->log->list)) != NULL) {
89+
while ((lxb_error = lexbor_array_obj_pop(&lexbor_parser.log->list)) != NULL) {
9090
zval error;
9191
object_init_ex(&error, uri_whatwg_url_validation_error_ce);
9292
zend_update_property_string(uri_whatwg_url_validation_error_ce, Z_OBJ(error), ZEND_STRL("context"), (const char *) lxb_error->data);
@@ -254,7 +254,7 @@ static zend_result lexbor_write_scheme(struct uri_internal_t *internal_uri, zval
254254

255255
zval_string_or_null_to_lexbor_str(value, &str);
256256

257-
if (lxb_url_api_protocol_set(lexbor_uri, lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
257+
if (lxb_url_api_protocol_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
258258
fill_errors(errors);
259259

260260
return FAILURE;
@@ -323,13 +323,13 @@ static zend_result lexbor_write_password(struct uri_internal_t *internal_uri, zv
323323

324324
static zend_result init_idna(void)
325325
{
326-
if (lexbor_parser->idna != NULL) {
326+
if (lexbor_parser.idna != NULL) {
327327
return SUCCESS;
328328
}
329329

330-
lexbor_parser->idna = lxb_unicode_idna_create();
330+
lexbor_parser.idna = lxb_unicode_idna_create();
331331

332-
return lxb_unicode_idna_init(lexbor_parser->idna) == LXB_STATUS_OK ? SUCCESS : FAILURE;
332+
return lxb_unicode_idna_init(lexbor_parser.idna) == LXB_STATUS_OK ? SUCCESS : FAILURE;
333333
}
334334

335335
static zend_result lexbor_read_host(const struct uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
@@ -357,8 +357,8 @@ static zend_result lexbor_read_host(const struct uri_internal_t *internal_uri, u
357357
if (init_idna() == FAILURE) {
358358
return FAILURE;
359359
}
360-
lxb_url_serialize_host_unicode(lexbor_parser->idna, &lexbor_uri->host, lexbor_serialize_callback, &host_str);
361-
lxb_unicode_idna_clean(lexbor_parser->idna);
360+
lxb_url_serialize_host_unicode(lexbor_parser.idna, &lexbor_uri->host, lexbor_serialize_callback, &host_str);
361+
lxb_unicode_idna_clean(lexbor_parser.idna);
362362

363363
ZVAL_NEW_STR(retval, smart_str_extract(&host_str));
364364
break;
@@ -384,7 +384,7 @@ static zend_result lexbor_write_host(struct uri_internal_t *internal_uri, zval *
384384

385385
zval_string_or_null_to_lexbor_str(value, &str);
386386

387-
if (lxb_url_api_hostname_set(lexbor_uri, lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
387+
if (lxb_url_api_hostname_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
388388
fill_errors(errors);
389389

390390
return FAILURE;
@@ -413,7 +413,7 @@ static zend_result lexbor_write_port(struct uri_internal_t *internal_uri, zval *
413413

414414
zval_long_or_null_to_lexbor_str(value, &str);
415415

416-
if (lxb_url_api_port_set(lexbor_uri, lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
416+
if (lxb_url_api_port_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
417417
fill_errors(errors);
418418

419419
return FAILURE;
@@ -442,7 +442,7 @@ static zend_result lexbor_write_path(struct uri_internal_t *internal_uri, zval *
442442

443443
zval_string_or_null_to_lexbor_str(value, &str);
444444

445-
if (lxb_url_api_pathname_set(lexbor_uri, lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
445+
if (lxb_url_api_pathname_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
446446
fill_errors(errors);
447447

448448
return FAILURE;
@@ -471,7 +471,7 @@ static zend_result lexbor_write_query(struct uri_internal_t *internal_uri, zval
471471

472472
zval_string_or_null_to_lexbor_str(value, &str);
473473

474-
if (lxb_url_api_search_set(lexbor_uri, lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
474+
if (lxb_url_api_search_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
475475
fill_errors(errors);
476476

477477
return FAILURE;
@@ -500,7 +500,7 @@ static zend_result lexbor_write_fragment(struct uri_internal_t *internal_uri, zv
500500

501501
zval_string_or_null_to_lexbor_str(value, &str);
502502

503-
if (lxb_url_api_hash_set(lexbor_uri, lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
503+
if (lxb_url_api_hash_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
504504
fill_errors(errors);
505505

506506
return FAILURE;
@@ -518,11 +518,9 @@ zend_result lexbor_request_init(void)
518518
return FAILURE;
519519
}
520520

521-
lexbor_parser = lxb_url_parser_create();
522-
status = lxb_url_parser_init(lexbor_parser, mraw);
521+
status = lxb_url_parser_init(&lexbor_parser, mraw);
523522
if (status != LXB_STATUS_OK) {
524-
lxb_url_parser_destroy(lexbor_parser, true);
525-
lexbor_parser = NULL;
523+
lxb_url_parser_destroy(&lexbor_parser, false);
526524
lexbor_mraw_destroy(mraw, true);
527525
return FAILURE;
528526
}
@@ -534,10 +532,9 @@ zend_result lexbor_request_init(void)
534532

535533
void lexbor_request_shutdown(void)
536534
{
537-
lxb_url_parser_memory_destroy(lexbor_parser);
538-
lxb_url_parser_destroy(lexbor_parser, true);
535+
lxb_url_parser_memory_destroy(&lexbor_parser);
536+
lxb_url_parser_destroy(&lexbor_parser, false);
539537

540-
lexbor_parser = NULL;
541538
lexbor_urls = 0;
542539
}
543540

@@ -546,7 +543,7 @@ static void *lexbor_parse_uri(const zend_string *uri_str, const void *base_url,
546543
lexbor_cleanup_parser();
547544

548545
const lxb_url_t *lexbor_base_url = base_url;
549-
lxb_url_t *url = lxb_url_parse(lexbor_parser, lexbor_base_url, (unsigned char *) ZSTR_VAL(uri_str), ZSTR_LEN(uri_str));
546+
lxb_url_t *url = lxb_url_parse(&lexbor_parser, lexbor_base_url, (unsigned char *) ZSTR_VAL(uri_str), ZSTR_LEN(uri_str));
550547
fill_errors(errors);
551548

552549
return url;
@@ -570,7 +567,7 @@ static void *lexbor_clone_uri(void *uri)
570567
{
571568
lxb_url_t *lexbor_uri = (lxb_url_t *) uri;
572569

573-
return lxb_url_clone(lexbor_parser->mraw, lexbor_uri);
570+
return lxb_url_clone(lexbor_parser.mraw, lexbor_uri);
574571
}
575572

576573
static zend_string *lexbor_uri_to_string(void *uri, uri_recomposition_mode_t recomposition_mode, bool exclude_fragment)
@@ -585,8 +582,8 @@ static zend_string *lexbor_uri_to_string(void *uri, uri_recomposition_mode_t rec
585582
if (init_idna() == FAILURE) {
586583
return NULL;
587584
}
588-
lxb_url_serialize_idna(lexbor_parser->idna, lexbor_uri, lexbor_serialize_callback, &uri_str, exclude_fragment);
589-
lxb_unicode_idna_clean(lexbor_parser->idna);
585+
lxb_url_serialize_idna(lexbor_parser.idna, lexbor_uri, lexbor_serialize_callback, &uri_str, exclude_fragment);
586+
lxb_unicode_idna_clean(lexbor_parser.idna);
590587
break;
591588
case URI_RECOMPOSITION_RAW_ASCII:
592589
ZEND_FALLTHROUGH;

0 commit comments

Comments
 (0)