Skip to content

Commit 707f785

Browse files
authored
uri: Inline parser and uri into uri_object_t (#19906)
* uri: Inline `uri_internal_from_obj()` and `Z_URI_INTERNAL_P()` Changes performed with Coccinelle with some minor adjustments in places where it choked due to macros: @@ expression e; @@ - Z_URI_INTERNAL_P(e) + &Z_URI_OBJECT_P(e)->internal @@ expression e; @@ - uri_internal_from_obj(e) + &uri_object_from_obj(e)->internal * uri: Inline definition of `URI_ASSERT_INITIALIZATION()` While a `NULL` pointer to `zend_object` would result in `->internal` also sitting at `0`, this is not a particularly useful assertion to have. Instead just assert that we have a parsed `->uri` available. Changes made with Coccinelle + some manual adjustments: @@ uri_internal_t *u; expression e; @@ u = &Z_URI_OBJECT_P(e)->internal ... when != u - URI_ASSERT_INITIALIZATION(u); + ZEND_ASSERT(u->uri != NULL); @@ uri_internal_t *u; expression e; @@ u = &uri_object_from_obj(e)->internal ... when != u - URI_ASSERT_INITIALIZATION(u); + ZEND_ASSERT(u->uri != NULL); * uri: Inline `parser` and `uri` into `uri_object_t` After this, `uri_internal_t` will only be used when interacting with the URI parsers without having a full-blown URI object. Changes made with Coccinelle and some manual adjustments: @@ identifier u; expression e; @@ - uri_internal_t *u = &e->internal; + uri_object_t *u = e; @@ uri_object_t *u; identifier t; @@ - u->internal.t + u->t * uri: Fix outdated `internal` naming for `uri_object_t` * uri: Clean up naming of `uri_object_t` variables
1 parent c79d8b6 commit 707f785

File tree

3 files changed

+78
-103
lines changed

3 files changed

+78
-103
lines changed

ext/uri/php_uri.c

Lines changed: 64 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,11 @@ static zend_array uri_parsers;
5353

5454
static HashTable *uri_get_debug_properties(uri_object_t *object)
5555
{
56-
uri_internal_t *internal_uri = &object->internal;
57-
ZEND_ASSERT(internal_uri != NULL);
58-
5956
const HashTable *std_properties = zend_std_get_properties(&object->std);
6057
HashTable *result = zend_array_dup(std_properties);
6158

62-
const php_uri_parser * const parser = internal_uri->parser;
63-
void * const uri = internal_uri->uri;
59+
const php_uri_parser * const parser = object->parser;
60+
void * const uri = object->uri;
6461

6562
if (UNEXPECTED(uri == NULL)) {
6663
return result;
@@ -324,7 +321,7 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2) PHPAPI void php_uri_instantiate_uri(
324321
uri_object_t *uri_object;
325322
if (should_update_this_object) {
326323
uri_object = Z_URI_OBJECT_P(ZEND_THIS);
327-
if (uri_object->internal.uri != NULL) {
324+
if (uri_object->uri != NULL) {
328325
zend_throw_error(NULL, "Cannot modify readonly object of class %s", ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name));
329326
RETURN_THROWS();
330327
}
@@ -337,18 +334,17 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2) PHPAPI void php_uri_instantiate_uri(
337334
uri_object = Z_URI_OBJECT_P(return_value);
338335
}
339336

340-
const php_uri_parser *uri_parser = uri_object->internal.parser;
337+
const php_uri_parser *uri_parser = uri_object->parser;
341338

342339
zval errors;
343340
ZVAL_UNDEF(&errors);
344341

345342
void *base_url = NULL;
346343
if (base_url_object != NULL) {
347344
ZEND_ASSERT(base_url_object->std.ce == uri_object->std.ce);
348-
const uri_internal_t *internal_base_url = &base_url_object->internal;
349-
URI_ASSERT_INITIALIZATION(internal_base_url);
350-
ZEND_ASSERT(internal_base_url->parser == uri_parser);
351-
base_url = internal_base_url->uri;
345+
ZEND_ASSERT(base_url_object->uri != NULL);
346+
ZEND_ASSERT(base_url_object->parser == uri_parser);
347+
base_url = base_url_object->uri;
352348
}
353349

354350
void *uri = uri_parser->parse(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str), base_url, errors_zv != NULL ? &errors : NULL, !should_throw);
@@ -370,7 +366,7 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2) PHPAPI void php_uri_instantiate_uri(
370366
RETURN_THROWS();
371367
}
372368

373-
uri_object->internal.uri = uri;
369+
uri_object->uri = uri;
374370
}
375371

376372
static void create_rfc3986_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor)
@@ -531,10 +527,10 @@ static void rfc3986_userinfo_read(INTERNAL_FUNCTION_PARAMETERS, php_uri_componen
531527
{
532528
ZEND_PARSE_PARAMETERS_NONE();
533529

534-
uri_internal_t *internal_uri = Z_URI_INTERNAL_P(ZEND_THIS);
535-
URI_ASSERT_INITIALIZATION(internal_uri);
530+
uri_object_t *uri_object = Z_URI_OBJECT_P(ZEND_THIS);
531+
ZEND_ASSERT(uri_object->uri != NULL);
536532

537-
if (UNEXPECTED(php_uri_parser_rfc3986_userinfo_read(internal_uri->uri, read_mode, return_value) == FAILURE)) {
533+
if (UNEXPECTED(php_uri_parser_rfc3986_userinfo_read(uri_object->uri, read_mode, return_value) == FAILURE)) {
538534
zend_throw_error(NULL, "The userinfo component cannot be retrieved");
539535
RETURN_THROWS();
540536
}
@@ -565,11 +561,10 @@ PHP_METHOD(Uri_Rfc3986_Uri, withUserInfo)
565561
ZVAL_STR(&zv, value);
566562
}
567563

568-
zend_object *old_object = Z_OBJ_P(ZEND_THIS);
569-
uri_internal_t *internal_uri = Z_URI_INTERNAL_P(ZEND_THIS);
570-
URI_ASSERT_INITIALIZATION(internal_uri);
564+
uri_object_t *old_uri_object = uri_object_from_obj(Z_OBJ_P(ZEND_THIS));
565+
ZEND_ASSERT(old_uri_object->uri != NULL);
571566

572-
zend_object *new_object = old_object->handlers->clone_obj(old_object);
567+
zend_object *new_object = old_uri_object->std.handlers->clone_obj(&old_uri_object->std);
573568
if (new_object == NULL) {
574569
RETURN_THROWS();
575570
}
@@ -578,10 +573,10 @@ PHP_METHOD(Uri_Rfc3986_Uri, withUserInfo)
578573
* case of an exception being thrown. */
579574
RETVAL_OBJ(new_object);
580575

581-
uri_internal_t *new_internal_uri = uri_internal_from_obj(new_object);
582-
URI_ASSERT_INITIALIZATION(new_internal_uri);
576+
uri_object_t *new_uri_object = uri_object_from_obj(new_object);
577+
ZEND_ASSERT(new_uri_object->uri != NULL);
583578

584-
if (UNEXPECTED(php_uri_parser_rfc3986_userinfo_write(new_internal_uri->uri, &zv, NULL) == FAILURE)) {
579+
if (UNEXPECTED(php_uri_parser_rfc3986_userinfo_write(new_uri_object->uri, &zv, NULL) == FAILURE)) {
585580
RETURN_THROWS();
586581
}
587582
}
@@ -684,11 +679,8 @@ static void throw_cannot_recompose_uri_to_string(uri_object_t *object)
684679
static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, uri_object_t *that_object, zend_object *comparison_mode)
685680
{
686681
uri_object_t *this_object = Z_URI_OBJECT_P(ZEND_THIS);
687-
uri_internal_t *this_internal_uri = &this_object->internal;
688-
URI_ASSERT_INITIALIZATION(this_internal_uri);
689-
690-
uri_internal_t *that_internal_uri = &that_object->internal;
691-
URI_ASSERT_INITIALIZATION(that_internal_uri);
682+
ZEND_ASSERT(this_object->uri != NULL);
683+
ZEND_ASSERT(that_object->uri != NULL);
692684

693685
if (this_object->std.ce != that_object->std.ce &&
694686
!instanceof_function(this_object->std.ce, that_object->std.ce) &&
@@ -703,15 +695,15 @@ static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, uri_object_t *that_object,
703695
exclude_fragment = zend_string_equals_literal(Z_STR_P(case_name), "ExcludeFragment");
704696
}
705697

706-
zend_string *this_str = this_internal_uri->parser->to_string(
707-
this_internal_uri->uri, PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII, exclude_fragment);
698+
zend_string *this_str = this_object->parser->to_string(
699+
this_object->uri, PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII, exclude_fragment);
708700
if (this_str == NULL) {
709701
throw_cannot_recompose_uri_to_string(this_object);
710702
RETURN_THROWS();
711703
}
712704

713-
zend_string *that_str = that_internal_uri->parser->to_string(
714-
that_internal_uri->uri, PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII, exclude_fragment);
705+
zend_string *that_str = that_object->parser->to_string(
706+
that_object->uri, PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII, exclude_fragment);
715707
if (that_str == NULL) {
716708
zend_string_release(this_str);
717709
throw_cannot_recompose_uri_to_string(that_object);
@@ -742,13 +734,12 @@ PHP_METHOD(Uri_Rfc3986_Uri, toRawString)
742734
{
743735
ZEND_PARSE_PARAMETERS_NONE();
744736

745-
uri_object_t *this_object = Z_URI_OBJECT_P(ZEND_THIS);
746-
uri_internal_t *internal_uri = &this_object->internal;
747-
URI_ASSERT_INITIALIZATION(internal_uri);
737+
uri_object_t *uri_object = Z_URI_OBJECT_P(ZEND_THIS);
738+
ZEND_ASSERT(uri_object->uri != NULL);
748739

749-
zend_string *uri_str = internal_uri->parser->to_string(internal_uri->uri, PHP_URI_RECOMPOSITION_MODE_RAW_ASCII, false);
740+
zend_string *uri_str = uri_object->parser->to_string(uri_object->uri, PHP_URI_RECOMPOSITION_MODE_RAW_ASCII, false);
750741
if (uri_str == NULL) {
751-
throw_cannot_recompose_uri_to_string(this_object);
742+
throw_cannot_recompose_uri_to_string(uri_object);
752743
RETURN_THROWS();
753744
}
754745

@@ -759,13 +750,12 @@ PHP_METHOD(Uri_Rfc3986_Uri, toString)
759750
{
760751
ZEND_PARSE_PARAMETERS_NONE();
761752

762-
uri_object_t *this_object = Z_URI_OBJECT_P(ZEND_THIS);
763-
uri_internal_t *internal_uri = &this_object->internal;
764-
URI_ASSERT_INITIALIZATION(internal_uri);
753+
uri_object_t *uri_object = Z_URI_OBJECT_P(ZEND_THIS);
754+
ZEND_ASSERT(uri_object->uri != NULL);
765755

766-
zend_string *uri_str = internal_uri->parser->to_string(internal_uri->uri, PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII, false);
756+
zend_string *uri_str = uri_object->parser->to_string(uri_object->uri, PHP_URI_RECOMPOSITION_MODE_NORMALIZED_ASCII, false);
767757
if (uri_str == NULL) {
768-
throw_cannot_recompose_uri_to_string(this_object);
758+
throw_cannot_recompose_uri_to_string(uri_object);
769759
RETURN_THROWS();
770760
}
771761

@@ -788,14 +778,13 @@ PHP_METHOD(Uri_Rfc3986_Uri, __serialize)
788778
{
789779
ZEND_PARSE_PARAMETERS_NONE();
790780

791-
uri_object_t *this_object = Z_URI_OBJECT_P(ZEND_THIS);
792-
uri_internal_t *internal_uri = &this_object->internal;
793-
URI_ASSERT_INITIALIZATION(internal_uri);
781+
uri_object_t *uri_object = Z_URI_OBJECT_P(ZEND_THIS);
782+
ZEND_ASSERT(uri_object->uri != NULL);
794783

795784
/* Serialize state: "uri" key in the first array */
796-
zend_string *uri_str = internal_uri->parser->to_string(internal_uri->uri, PHP_URI_RECOMPOSITION_MODE_RAW_ASCII, false);
785+
zend_string *uri_str = uri_object->parser->to_string(uri_object->uri, PHP_URI_RECOMPOSITION_MODE_RAW_ASCII, false);
797786
if (uri_str == NULL) {
798-
throw_cannot_recompose_uri_to_string(this_object);
787+
throw_cannot_recompose_uri_to_string(uri_object);
799788
RETURN_THROWS();
800789
}
801790
zval tmp;
@@ -809,7 +798,7 @@ PHP_METHOD(Uri_Rfc3986_Uri, __serialize)
809798
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &arr);
810799

811800
/* Serialize regular properties: second array */
812-
ZVAL_ARR(&arr, this_object->std.handlers->get_properties(&this_object->std));
801+
ZVAL_ARR(&arr, uri_object->std.handlers->get_properties(&uri_object->std));
813802
Z_TRY_ADDREF(arr);
814803
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &arr);
815804
}
@@ -822,56 +811,55 @@ static void uri_unserialize(INTERNAL_FUNCTION_PARAMETERS)
822811
Z_PARAM_ARRAY_HT(data)
823812
ZEND_PARSE_PARAMETERS_END();
824813

825-
zend_object *object = Z_OBJ_P(ZEND_THIS);
826-
uri_internal_t *internal_uri = uri_internal_from_obj(object);
827-
if (internal_uri->uri != NULL) {
814+
uri_object_t *uri_object = uri_object_from_obj(Z_OBJ_P(ZEND_THIS));
815+
if (uri_object->uri != NULL) {
828816
/* Intentionally throw two exceptions for proper chaining. */
829-
zend_throw_error(NULL, "Cannot modify readonly object of class %s", ZSTR_VAL(object->ce->name));
830-
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name));
817+
zend_throw_error(NULL, "Cannot modify readonly object of class %s", ZSTR_VAL(uri_object->std.ce->name));
818+
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name));
831819
RETURN_THROWS();
832820
}
833821

834822
/* Verify the expected number of elements, this implicitly ensures that no additional elements are present. */
835823
if (zend_hash_num_elements(data) != 2) {
836-
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name));
824+
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name));
837825
RETURN_THROWS();
838826
}
839827

840828
/* Unserialize state: "uri" key in the first array */
841829
zval *arr = zend_hash_index_find(data, 0);
842830
if (arr == NULL || Z_TYPE_P(arr) != IS_ARRAY) {
843-
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name));
831+
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name));
844832
RETURN_THROWS();
845833
}
846834

847835
/* Verify the expected number of elements inside the first array, this implicitly ensures that no additional elements are present. */
848836
if (zend_hash_num_elements(Z_ARRVAL_P(arr)) != 1) {
849-
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name));
837+
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name));
850838
RETURN_THROWS();
851839
}
852840

853841
zval *uri_zv = zend_hash_str_find_ind(Z_ARRVAL_P(arr), ZEND_STRL(URI_SERIALIZED_PROPERTY_NAME));
854842
if (uri_zv == NULL || Z_TYPE_P(uri_zv) != IS_STRING) {
855-
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name));
843+
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name));
856844
RETURN_THROWS();
857845
}
858846

859-
internal_uri->uri = internal_uri->parser->parse(Z_STRVAL_P(uri_zv), Z_STRLEN_P(uri_zv), NULL, NULL, true);
860-
if (internal_uri->uri == NULL) {
861-
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name));
847+
uri_object->uri = uri_object->parser->parse(Z_STRVAL_P(uri_zv), Z_STRLEN_P(uri_zv), NULL, NULL, true);
848+
if (uri_object->uri == NULL) {
849+
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name));
862850
RETURN_THROWS();
863851
}
864852

865853
/* Unserialize regular properties: second array */
866854
arr = zend_hash_index_find(data, 1);
867855
if (arr == NULL || Z_TYPE_P(arr) != IS_ARRAY) {
868-
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name));
856+
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name));
869857
RETURN_THROWS();
870858
}
871859

872860
/* Verify that there is no regular property in the second array, because the URI classes have no properties and they are final. */
873861
if (zend_hash_num_elements(Z_ARRVAL_P(arr)) > 0) {
874-
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name));
862+
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(uri_object->std.ce->name));
875863
RETURN_THROWS();
876864
}
877865
}
@@ -944,21 +932,21 @@ PHP_METHOD(Uri_WhatWg_Url, toUnicodeString)
944932
ZEND_PARSE_PARAMETERS_NONE();
945933

946934
zend_object *this_object = Z_OBJ_P(ZEND_THIS);
947-
uri_internal_t *internal_uri = uri_internal_from_obj(this_object);
948-
URI_ASSERT_INITIALIZATION(internal_uri);
935+
uri_object_t *uri_object = uri_object_from_obj(this_object);
936+
ZEND_ASSERT(uri_object->uri != NULL);
949937

950-
RETURN_STR(internal_uri->parser->to_string(internal_uri->uri, PHP_URI_RECOMPOSITION_MODE_RAW_UNICODE, false));
938+
RETURN_STR(uri_object->parser->to_string(uri_object->uri, PHP_URI_RECOMPOSITION_MODE_RAW_UNICODE, false));
951939
}
952940

953941
PHP_METHOD(Uri_WhatWg_Url, toAsciiString)
954942
{
955943
ZEND_PARSE_PARAMETERS_NONE();
956944

957945
zend_object *this_object = Z_OBJ_P(ZEND_THIS);
958-
uri_internal_t *internal_uri = uri_internal_from_obj(this_object);
959-
URI_ASSERT_INITIALIZATION(internal_uri);
946+
uri_object_t *uri_object = uri_object_from_obj(this_object);
947+
ZEND_ASSERT(uri_object->uri != NULL);
960948

961-
RETURN_STR(internal_uri->parser->to_string(internal_uri->uri, PHP_URI_RECOMPOSITION_MODE_RAW_ASCII, false));
949+
RETURN_STR(uri_object->parser->to_string(uri_object->uri, PHP_URI_RECOMPOSITION_MODE_RAW_ASCII, false));
962950
}
963951

964952
PHP_METHOD(Uri_WhatWg_Url, resolve)
@@ -981,11 +969,10 @@ PHP_METHOD(Uri_WhatWg_Url, __serialize)
981969
ZEND_PARSE_PARAMETERS_NONE();
982970

983971
uri_object_t *this_object = Z_URI_OBJECT_P(ZEND_THIS);
984-
uri_internal_t *internal_uri = &this_object->internal;
985-
URI_ASSERT_INITIALIZATION(internal_uri);
972+
ZEND_ASSERT(this_object->uri != NULL);
986973

987974
/* Serialize state: "uri" key in the first array */
988-
zend_string *uri_str = internal_uri->parser->to_string(internal_uri->uri, PHP_URI_RECOMPOSITION_MODE_RAW_ASCII, false);
975+
zend_string *uri_str = this_object->parser->to_string(this_object->uri, PHP_URI_RECOMPOSITION_MODE_RAW_ASCII, false);
989976
if (uri_str == NULL) {
990977
throw_cannot_recompose_uri_to_string(this_object);
991978
RETURN_THROWS();
@@ -1027,10 +1014,8 @@ PHPAPI uri_object_t *php_uri_object_create(zend_class_entry *class_type, const p
10271014
zend_object_std_init(&uri_object->std, class_type);
10281015
object_properties_init(&uri_object->std, class_type);
10291016

1030-
uri_object->internal = (uri_internal_t){
1031-
.parser = parser,
1032-
.uri = NULL,
1033-
};
1017+
uri_object->parser = parser;
1018+
uri_object->uri = NULL;
10341019

10351020
return uri_object;
10361021
}
@@ -1049,24 +1034,23 @@ PHPAPI void php_uri_object_handler_free(zend_object *object)
10491034
{
10501035
uri_object_t *uri_object = uri_object_from_obj(object);
10511036

1052-
uri_object->internal.parser->destroy(uri_object->internal.uri);
1037+
uri_object->parser->destroy(uri_object->uri);
10531038
zend_object_std_dtor(&uri_object->std);
10541039
}
10551040

10561041
PHPAPI zend_object *php_uri_object_handler_clone(zend_object *object)
10571042
{
10581043
uri_object_t *uri_object = uri_object_from_obj(object);
1059-
uri_internal_t *internal_uri = uri_internal_from_obj(object);
10601044

1061-
URI_ASSERT_INITIALIZATION(internal_uri);
1045+
ZEND_ASSERT(uri_object->uri != NULL);
10621046

10631047
uri_object_t *new_uri_object = uri_object_from_obj(object->ce->create_object(object->ce));
1064-
ZEND_ASSERT(new_uri_object->internal.parser == internal_uri->parser);
1048+
ZEND_ASSERT(new_uri_object->parser == uri_object->parser);
10651049

1066-
void *uri = internal_uri->parser->clone(internal_uri->uri);
1050+
void *uri = uri_object->parser->clone(uri_object->uri);
10671051
ZEND_ASSERT(uri != NULL);
10681052

1069-
new_uri_object->internal.uri = uri;
1053+
new_uri_object->uri = uri;
10701054

10711055
zend_objects_clone_members(&new_uri_object->std, &uri_object->std);
10721056

0 commit comments

Comments
 (0)