Skip to content

Commit d2a54a0

Browse files
authored
uri: Do not treat arbitrary error codes as syntax errors in uri_parser_rfc3986 (#19779)
In case one of the other error codes is returned, treating it as a syntax error is going to be confusing for the user (and also the PHP team when an issue is reported).
1 parent ded813b commit d2a54a0

File tree

1 file changed

+66
-25
lines changed

1 file changed

+66
-25
lines changed

ext/uri/uri_parser_rfc3986.c

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,17 @@ static zend_result php_uri_parser_rfc3986_scheme_write(struct uri_internal_t *in
130130
result = uriSetSchemeMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm);
131131
}
132132

133-
if (result != URI_SUCCESS) {
134-
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified scheme is malformed", 0);
135-
return FAILURE;
133+
switch (result) {
134+
case URI_SUCCESS:
135+
return SUCCESS;
136+
case URI_ERROR_SYNTAX:
137+
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified scheme is malformed", 0);
138+
return FAILURE;
139+
default:
140+
/* This should be unreachable in practice. */
141+
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the scheme", 0);
142+
return FAILURE;
136143
}
137-
138-
return SUCCESS;
139144
}
140145

141146
ZEND_ATTRIBUTE_NONNULL zend_result php_uri_parser_rfc3986_userinfo_read(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
@@ -168,9 +173,13 @@ zend_result php_uri_parser_rfc3986_userinfo_write(struct uri_internal_t *interna
168173
case URI_ERROR_SETUSERINFO_HOST_NOT_SET:
169174
zend_throw_exception(uri_invalid_uri_exception_ce, "Cannot set a userinfo without having a host", 0);
170175
return FAILURE;
171-
default:
176+
case URI_ERROR_SYNTAX:
172177
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified userinfo is malformed", 0);
173178
return FAILURE;
179+
default:
180+
/* This should be unreachable in practice. */
181+
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the userinfo", 0);
182+
return FAILURE;
174183
}
175184
}
176185

@@ -259,9 +268,13 @@ static zend_result php_uri_parser_rfc3986_host_write(struct uri_internal_t *inte
259268
case URI_ERROR_SETHOST_USERINFO_SET:
260269
zend_throw_exception(uri_invalid_uri_exception_ce, "Cannot remove the host from a URI that has a userinfo", 0);
261270
return FAILURE;
262-
default:
271+
case URI_ERROR_SYNTAX:
263272
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified host is malformed", 0);
264273
return FAILURE;
274+
default:
275+
/* This should be unreachable in practice. */
276+
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the host", 0);
277+
return FAILURE;
265278
}
266279
}
267280

@@ -315,9 +328,13 @@ static zend_result php_uri_parser_rfc3986_port_write(struct uri_internal_t *inte
315328
case URI_ERROR_SETPORT_HOST_NOT_SET:
316329
zend_throw_exception(uri_invalid_uri_exception_ce, "Cannot set a port without having a host", 0);
317330
return FAILURE;
318-
default:
331+
case URI_ERROR_SYNTAX:
319332
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified port is malformed", 0);
320333
return FAILURE;
334+
default:
335+
/* This should be unreachable in practice. */
336+
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the port", 0);
337+
return FAILURE;
321338
}
322339
}
323340

@@ -360,12 +377,17 @@ static zend_result php_uri_parser_rfc3986_path_write(struct uri_internal_t *inte
360377
result = uriSetPathMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm);
361378
}
362379

363-
if (result != URI_SUCCESS) {
364-
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified path is malformed", 0);
365-
return FAILURE;
380+
switch (result) {
381+
case URI_SUCCESS:
382+
return SUCCESS;
383+
case URI_ERROR_SYNTAX:
384+
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified path is malformed", 0);
385+
return FAILURE;
386+
default:
387+
/* This should be unreachable in practice. */
388+
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the path", 0);
389+
return FAILURE;
366390
}
367-
368-
return SUCCESS;
369391
}
370392

371393
ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_query_read(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
@@ -392,12 +414,17 @@ static zend_result php_uri_parser_rfc3986_query_write(struct uri_internal_t *int
392414
result = uriSetQueryMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm);
393415
}
394416

395-
if (result != URI_SUCCESS) {
396-
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified query is malformed", 0);
397-
return FAILURE;
417+
switch (result) {
418+
case URI_SUCCESS:
419+
return SUCCESS;
420+
case URI_ERROR_SYNTAX:
421+
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified query is malformed", 0);
422+
return FAILURE;
423+
default:
424+
/* This should be unreachable in practice. */
425+
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the query", 0);
426+
return FAILURE;
398427
}
399-
400-
return SUCCESS;
401428
}
402429

403430
ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_fragment_read(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
@@ -424,12 +451,17 @@ static zend_result php_uri_parser_rfc3986_fragment_write(struct uri_internal_t *
424451
result = uriSetFragmentMmA(uriparser_uri, Z_STRVAL_P(value), Z_STRVAL_P(value) + Z_STRLEN_P(value), mm);
425452
}
426453

427-
if (result != URI_SUCCESS) {
428-
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified fragment is malformed", 0);
429-
return FAILURE;
454+
switch (result) {
455+
case URI_SUCCESS:
456+
return SUCCESS;
457+
case URI_ERROR_SYNTAX:
458+
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified fragment is malformed", 0);
459+
return FAILURE;
460+
default:
461+
/* This should be unreachable in practice. */
462+
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to update the fragment", 0);
463+
return FAILURE;
430464
}
431-
432-
return SUCCESS;
433465
}
434466

435467
static php_uri_parser_rfc3986_uris *uriparser_create_uris(void)
@@ -445,9 +477,18 @@ php_uri_parser_rfc3986_uris *php_uri_parser_rfc3986_parse_ex(const char *uri_str
445477
UriUriA uri = {0};
446478

447479
/* Parse the URI. */
448-
if (uriParseSingleUriExMmA(&uri, uri_str, uri_str + uri_str_len, NULL, mm) != URI_SUCCESS) {
480+
int result = uriParseSingleUriExMmA(&uri, uri_str, uri_str + uri_str_len, NULL, mm);
481+
if (result != URI_SUCCESS) {
449482
if (!silent) {
450-
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified URI is malformed", 0);
483+
switch (result) {
484+
case URI_ERROR_SYNTAX:
485+
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified URI is malformed", 0);
486+
break;
487+
default:
488+
/* This should be unreachable in practice. */
489+
zend_throw_exception(uri_invalid_uri_exception_ce, "Failed to parse the specified URI", 0);
490+
break;
491+
}
451492
}
452493

453494
goto fail;

0 commit comments

Comments
 (0)