Skip to content

Commit 352c68f

Browse files
committed
Another review round
1 parent 31c50e7 commit 352c68f

File tree

6 files changed

+149
-115
lines changed

6 files changed

+149
-115
lines changed

Zend/zend_exceptions.c

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -329,42 +329,51 @@ ZEND_COLD ZEND_METHOD(Exception, __clone)
329329
}
330330
/* }}} */
331331

332-
/* {{{ Exception constructor */
333-
ZEND_METHOD(Exception, __construct)
332+
ZEND_API zend_result zend_update_exception_properties(INTERNAL_FUNCTION_PARAMETERS, zend_string *message, zend_long code, zval *previous)
334333
{
335-
zend_string *message = NULL;
336-
zend_long code = 0;
337-
zval tmp, *object, *previous = NULL;
338-
339-
object = ZEND_THIS;
340-
341-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) {
342-
RETURN_THROWS();
343-
}
334+
zval tmp, *object = ZEND_THIS;
344335

345336
if (message) {
346337
ZVAL_STR_COPY(&tmp, message);
347338
zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_MESSAGE_OFF, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
348339
if (UNEXPECTED(EG(exception))) {
349-
RETURN_THROWS();
340+
return FAILURE;
350341
}
351342
}
352343

353344
if (code) {
354345
ZVAL_LONG(&tmp, code);
355346
zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_CODE_OFF, ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
356347
if (UNEXPECTED(EG(exception))) {
357-
RETURN_THROWS();
348+
return FAILURE;
358349
}
359350
}
360351

361352
if (previous) {
362353
Z_ADDREF_P(previous);
363354
zend_update_property_num_checked(zend_ce_exception, Z_OBJ_P(object), ZEND_EXCEPTION_PREVIOUS_OFF, ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
364355
if (UNEXPECTED(EG(exception))) {
365-
RETURN_THROWS();
356+
return FAILURE;
366357
}
367358
}
359+
360+
return SUCCESS;
361+
}
362+
363+
/* {{{ Exception constructor */
364+
ZEND_METHOD(Exception, __construct)
365+
{
366+
zend_string *message = NULL;
367+
zend_long code = 0;
368+
zval *previous = NULL;
369+
370+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) {
371+
RETURN_THROWS();
372+
}
373+
374+
if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) {
375+
RETURN_THROWS();
376+
}
368377
}
369378
/* }}} */
370379

@@ -401,28 +410,8 @@ ZEND_METHOD(ErrorException, __construct)
401410

402411
object = ZEND_THIS;
403412

404-
if (message) {
405-
ZVAL_STR_COPY(&tmp, message);
406-
zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_MESSAGE_OFF, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
407-
if (UNEXPECTED(EG(exception))) {
408-
RETURN_THROWS();
409-
}
410-
}
411-
412-
if (code) {
413-
ZVAL_LONG(&tmp, code);
414-
zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_CODE_OFF, ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
415-
if (UNEXPECTED(EG(exception))) {
416-
RETURN_THROWS();
417-
}
418-
}
419-
420-
if (previous) {
421-
Z_ADDREF_P(previous);
422-
zend_update_property_num_checked(zend_ce_exception, Z_OBJ_P(object), ZEND_EXCEPTION_PREVIOUS_OFF, ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
423-
if (UNEXPECTED(EG(exception))) {
424-
RETURN_THROWS();
425-
}
413+
if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) {
414+
RETURN_THROWS();
426415
}
427416

428417
ZVAL_LONG(&tmp, severity);

Zend/zend_exceptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ ZEND_API zend_object *zend_throw_error_exception(zend_class_entry *exception_ce,
6969

7070
extern ZEND_API void (*zend_throw_exception_hook)(zend_object *ex);
7171

72+
ZEND_API zend_result zend_update_exception_properties(INTERNAL_FUNCTION_PARAMETERS, zend_string *message, zend_long code, zval *previous);
73+
7274
/* show an exception using zend_error(severity,...), severity should be E_ERROR */
7375
ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *exception, int severity);
7476
ZEND_NORETURN void zend_exception_uncaught_error(const char *prefix, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2);

ext/uri/php_lexbor.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,20 @@ static void lexbor_cleanup_parser(void)
6767

6868
/**
6969
* Creates a Uri\WhatWg\UrlValidationError class by mapping error codes listed in
70-
* https://url.spec.whatwg.org/#writing to a Uri\WhatWg\UrlValidationErrorType enum
70+
* https://url.spec.whatwg.org/#writing to a Uri\WhatWg\UrlValidationErrorType enum.
71+
* The result is passed by reference to the errors parameter.
72+
*
73+
* When errors is NULL, the caller is not interested in the additional error information,
74+
* so the function does nothing.
7175
*/
7276
static void fill_errors(zval *errors)
7377
{
7478
if (errors == NULL) {
7579
return;
7680
}
7781

82+
ZEND_ASSERT(Z_ISUNDEF_P(errors));
83+
7884
array_init(errors);
7985

8086
if (lexbor_parser->log == NULL) {
@@ -550,6 +556,8 @@ static void *lexbor_parse_uri(const zend_string *uri_str, const void *base_url,
550556

551557
static void lexbor_create_invalid_uri_exception(zval *exception_zv, zval *errors)
552558
{
559+
ZEND_ASSERT(Z_TYPE_P(errors) == IS_ARRAY && "Lexbor always returns an array of errors when URI parsing fails");
560+
553561
object_init_ex(exception_zv, uri_whatwg_invalid_url_exception_ce);
554562

555563
zval value;

ext/uri/php_uri.c

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,12 @@ PHP_METHOD(Uri_WhatWg_InvalidUrlException, __construct)
119119
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(previous, zend_ce_throwable)
120120
ZEND_PARSE_PARAMETERS_END();
121121

122-
zval tmp;
123-
if (message != NULL) {
124-
ZVAL_STR(&tmp, message);
125-
zend_update_property_ex(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
126-
if (EG(exception)) {
127-
RETURN_THROWS();
128-
}
122+
if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) {
123+
RETURN_THROWS();
129124
}
130125

131126
if (errors == NULL) {
127+
zval tmp;
132128
ZVAL_EMPTY_ARRAY(&tmp);
133129
zend_update_property(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("errors"), &tmp);
134130
} else {
@@ -137,21 +133,6 @@ PHP_METHOD(Uri_WhatWg_InvalidUrlException, __construct)
137133
if (EG(exception)) {
138134
RETURN_THROWS();
139135
}
140-
141-
if (code != 0) {
142-
ZVAL_LONG(&tmp, code);
143-
zend_update_property_ex(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
144-
if (EG(exception)) {
145-
RETURN_THROWS();
146-
}
147-
}
148-
149-
if (previous != NULL) {
150-
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
151-
if (EG(exception)) {
152-
RETURN_THROWS();
153-
}
154-
}
155136
}
156137

157138
PHP_METHOD(Uri_WhatWg_UrlValidationError, __construct)
@@ -184,20 +165,30 @@ PHP_METHOD(Uri_WhatWg_UrlValidationError, __construct)
184165
}
185166
}
186167

187-
static zend_result pass_errors_by_ref(zval *errors_zv, zval *errors)
168+
/**
169+
* Pass the errors parameter by ref to errors_zv for userland, and frees it if
170+
* it is not not needed anymore.
171+
*/
172+
static zend_result pass_errors_by_ref_and_free(zval *errors_zv, zval *errors)
188173
{
189174
ZEND_ASSERT(Z_TYPE_P(errors) == IS_UNDEF || Z_TYPE_P(errors) == IS_ARRAY);
190175

176+
/* There was no error during parsing */
191177
if (Z_ISUNDEF_P(errors)) {
192178
return SUCCESS;
193179
}
194180

181+
/* The errors parameter is an array, but the pass-by ref argument stored by
182+
* errors_zv was not passed - the URI implementation either doesn't support
183+
* returning additional error information, or the caller is not interested in it */
195184
if (errors_zv == NULL) {
185+
zval_ptr_dtor(errors);
196186
return SUCCESS;
197187
}
198188

199189
ZEND_TRY_ASSIGN_REF_ARR(errors_zv, Z_ARRVAL_P(errors));
200190
if (EG(exception)) {
191+
zval_ptr_dtor(errors);
201192
return FAILURE;
202193
}
203194

@@ -225,14 +216,14 @@ PHPAPI void php_uri_instantiate_uri(
225216
zval_ptr_dtor(&errors);
226217
RETURN_THROWS();
227218
} else {
228-
if (pass_errors_by_ref(errors_zv, &errors) == FAILURE) {
219+
if (pass_errors_by_ref_and_free(errors_zv, &errors) == FAILURE) {
229220
RETURN_THROWS();
230221
}
231222
RETURN_NULL();
232223
}
233224
}
234225

235-
if (pass_errors_by_ref(errors_zv, &errors) == FAILURE) {
226+
if (pass_errors_by_ref_and_free(errors_zv, &errors) == FAILURE) {
236227
RETURN_THROWS();
237228
}
238229

@@ -388,93 +379,87 @@ static void uri_unserialize(INTERNAL_FUNCTION_PARAMETERS, const char *handler_na
388379

389380
PHP_METHOD(Uri_WhatWg_Url, getScheme)
390381
{
391-
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_SCHEME),
392-
URI_COMPONENT_READ_NORMALIZED_ASCII);
382+
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_SCHEME, URI_COMPONENT_READ_NORMALIZED_ASCII);
393383
}
394384

395385
PHP_METHOD(Uri_WhatWg_Url, withScheme)
396386
{
397-
uri_write_component_str(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_SCHEME));
387+
uri_write_component_str(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_SCHEME);
398388
}
399389

400390
PHP_METHOD(Uri_WhatWg_Url, getUsername)
401391
{
402-
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_USERNAME),
403-
URI_COMPONENT_READ_NORMALIZED_ASCII);
392+
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_USERNAME, URI_COMPONENT_READ_NORMALIZED_ASCII);
404393
}
405394

406395
PHP_METHOD(Uri_WhatWg_Url, withUsername)
407396
{
408-
uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_USERNAME));
397+
uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_USERNAME);
409398
}
410399

411400
PHP_METHOD(Uri_WhatWg_Url, getPassword)
412401
{
413-
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_PASSWORD),
414-
URI_COMPONENT_READ_NORMALIZED_ASCII);
402+
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_PASSWORD, URI_COMPONENT_READ_NORMALIZED_ASCII);
415403
}
416404

417405
PHP_METHOD(Uri_WhatWg_Url, withPassword)
418406
{
419-
uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_PASSWORD));
407+
uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_PASSWORD);
420408
}
421409

422410
PHP_METHOD(Uri_WhatWg_Url, getAsciiHost)
423411
{
424-
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_HOST), URI_COMPONENT_READ_NORMALIZED_ASCII);
412+
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_HOST, URI_COMPONENT_READ_NORMALIZED_ASCII);
425413
}
426414

427415
PHP_METHOD(Uri_WhatWg_Url, getUnicodeHost)
428416
{
429-
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_HOST),
430-
URI_COMPONENT_READ_NORMALIZED_UNICODE);
417+
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_HOST, URI_COMPONENT_READ_NORMALIZED_UNICODE);
431418
}
432419

433420
PHP_METHOD(Uri_WhatWg_Url, withHost)
434421
{
435-
uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_HOST));
422+
uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_HOST);
436423
}
437424

438425
PHP_METHOD(Uri_WhatWg_Url, getPort)
439426
{
440-
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_PORT), URI_COMPONENT_READ_NORMALIZED_ASCII);
427+
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_PORT, URI_COMPONENT_READ_NORMALIZED_ASCII);
441428
}
442429

443430
PHP_METHOD(Uri_WhatWg_Url, withPort)
444431
{
445-
uri_write_component_long_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_PORT));
432+
uri_write_component_long_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_PORT);
446433
}
447434

448435
PHP_METHOD(Uri_WhatWg_Url, getPath)
449436
{
450-
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_PATH), URI_COMPONENT_READ_NORMALIZED_ASCII);
437+
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_PATH, URI_COMPONENT_READ_NORMALIZED_ASCII);
451438
}
452439

453440
PHP_METHOD(Uri_WhatWg_Url, withPath)
454441
{
455-
uri_write_component_str(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_PATH));
442+
uri_write_component_str(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_PATH);
456443
}
457444

458445
PHP_METHOD(Uri_WhatWg_Url, getQuery)
459446
{
460-
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_QUERY),
461-
URI_COMPONENT_READ_NORMALIZED_ASCII);
447+
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_QUERY, URI_COMPONENT_READ_NORMALIZED_ASCII);
462448
}
463449

464450
PHP_METHOD(Uri_WhatWg_Url, withQuery)
465451
{
466-
uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_QUERY));
452+
uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_QUERY);
467453
}
468454

469455
PHP_METHOD(Uri_WhatWg_Url, getFragment)
470456
{
471-
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_FRAGMENT),
472-
URI_COMPONENT_READ_NORMALIZED_ASCII);
457+
uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_FRAGMENT, URI_COMPONENT_READ_NORMALIZED_ASCII);
473458
}
474459

475460
PHP_METHOD(Uri_WhatWg_Url, withFragment)
476461
{
477-
uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZSTR_KNOWN(ZEND_STR_FRAGMENT));
462+
uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_FRAGMENT);
478463
}
479464

480465
PHP_METHOD(Uri_WhatWg_Url, equals)
@@ -557,7 +542,7 @@ PHP_METHOD(Uri_WhatWg_Url, __serialize)
557542

558543
/* Serialize regular properties: second array */
559544
ZVAL_ARR(&arr, this_object->handlers->get_properties(this_object));
560-
Z_TRY_ADDREF(arr);
545+
Z_ADDREF(arr);
561546
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &arr);
562547
}
563548

0 commit comments

Comments
 (0)