Skip to content

ext/xml: Deprecate xml_set_object() and passing non-callable strings to handlers #15293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ PHP NEWS
. Implemented GH-15155 (Stream context is lost when custom stream wrapper is
being filtered). (Quentin Dreyer)

- XML:
. The xml_set_object() function has been deprecated. (Girgias)
. Passing non-callable strings to the xml_set_*_handler() functions is now
deprecated. (Girgias)

01 Aug 2024, PHP 8.4.0alpha4

- GMP:
Expand Down
9 changes: 8 additions & 1 deletion UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ PHP 8.4 UPGRADE NOTES
This means that xml_set_object() must now always be called prior to setting
method names as callables.
Passing an empty string to disable the handler is still allowed,
but not recommended.
but deprecated.

- XMLReader:
. Passing an invalid character encoding to XMLReader::open() or
Expand Down Expand Up @@ -474,6 +474,13 @@ PHP 8.4 UPGRADE NOTES
. Unserializing strings using the uppercase 'S' tag is deprecated.
RFC: https://wiki.php.net/rfc/deprecations_php_8_4

- XML:
. The xml_set_object() function has been deprecated.
RFC: https://wiki.php.net/rfc/deprecations_php_8_4#xml_set_object_and_xml_set_handler_with_string_method_names
. Passing non-callable strings to the xml_set_*_handler() functions is now
deprecated.
RFC: https://wiki.php.net/rfc/deprecations_php_8_4#xml_set_object_and_xml_set_handler_with_string_method_names

========================================
5. Changed Functions
========================================
Expand Down
3 changes: 2 additions & 1 deletion Zend/tests/bug34617.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ function boom()
}
boom();
?>
--EXPECT--
--EXPECTF--
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
ok
51 changes: 0 additions & 51 deletions ext/xml/tests/bug30266.phpt

This file was deleted.

3 changes: 1 addition & 2 deletions ext/xml/tests/bug32001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ HERE;

$parser = xml_parser_create(NULL);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_object($parser, $this);
xml_set_element_handler($parser, "start_element", "end_element");
xml_set_element_handler($parser, $this->start_element(...), $this->end_element(...));

if ($this->chunk_size == 0) {
$success = @xml_parse($parser, $data, true);
Expand Down
3 changes: 2 additions & 1 deletion ext/xml/tests/bug72793.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ $xml_parser->free();

?>
===DONE===
--EXPECT--
--EXPECTF--
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
===DONE===
10 changes: 9 additions & 1 deletion ext/xml/tests/set_element_handler_trampoline.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ xml_parse($parser, $xml, true);
xml_parser_free($parser);

?>
--EXPECT--
--EXPECTF--
Both handlers are trampolines:
Trampoline for start_handler
Tag: A
Expand All @@ -75,6 +75,10 @@ Trampoline for end_handler
Tag: A

Start handler is trampoline, end handler method string:

Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d

Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
Trampoline for start_handler
Tag: A
Trampoline for start_handler
Expand All @@ -86,6 +90,10 @@ Method end handler: C
Method end handler: A

End handler is trampoline, start handler method string:

Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d

Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
Method start handler: A
Method start handler: B
Trampoline for end_handler
Expand Down
8 changes: 7 additions & 1 deletion ext/xml/tests/set_handler_errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ try {
}

?>
--EXPECT--
--EXPECTF--
Invalid $parser:
TypeError: xml_set_processing_instruction_handler(): Argument #1 ($parser) must be of type XMLParser, stdClass given
Invalid callable type true:
TypeError: xml_set_processing_instruction_handler(): Argument #2 ($handler) must be of type callable|string|null
Invalid callable type int:
TypeError: xml_set_processing_instruction_handler(): Argument #2 ($handler) must be of type callable|string|null
String not callable and no object set:

Deprecated: xml_set_processing_instruction_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
ValueError: xml_set_processing_instruction_handler(): Argument #2 ($handler) an object must be set via xml_set_object() to be able to lookup method
String non existent method on set object:

Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d

Deprecated: xml_set_processing_instruction_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
ValueError: xml_set_processing_instruction_handler(): Argument #2 ($handler) method stdClass::nonexistent_method() does not exist
14 changes: 13 additions & 1 deletion ext/xml/tests/xml_set_element_handler_errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ try {
}

?>
--EXPECT--
--EXPECTF--
Invalid $parser:
TypeError: xml_set_element_handler(): Argument #1 ($parser) must be of type XMLParser, stdClass given
Invalid start callable type true:
Expand All @@ -86,10 +86,22 @@ TypeError: xml_set_element_handler(): Argument #2 ($start_handler) must be of ty
Invalid end callable type int:
TypeError: xml_set_element_handler(): Argument #3 ($end_handler) must be of type callable|string|null
Invalid start callable, no object set and string not callable:

Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
ValueError: xml_set_element_handler(): Argument #2 ($start_handler) an object must be set via xml_set_object() to be able to lookup method
Invalid end callable, no object set and string not callable:

Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
ValueError: xml_set_element_handler(): Argument #3 ($end_handler) an object must be set via xml_set_object() to be able to lookup method
Invalid start callable, string non existent method on set object:

Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d

Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
ValueError: xml_set_element_handler(): Argument #2 ($start_handler) method stdClass::nonexistent_method() does not exist
Invalid end callable, string non existent method on set object:

Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d

Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
ValueError: xml_set_element_handler(): Argument #3 ($end_handler) method stdClass::nonexistent_method() does not exist
5 changes: 2 additions & 3 deletions ext/xml/tests/xml_set_notation_decl_handler_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ class XML_Parser
function parse($data)
{
$parser = xml_parser_create();
xml_set_object($parser, $this);
xml_set_notation_decl_handler($parser, "notation_decl_handler");
xml_set_unparsed_entity_decl_handler($parser, "unparsed_entity_decl_handler");
xml_set_notation_decl_handler($parser, $this->notation_decl_handler(...));
xml_set_unparsed_entity_decl_handler($parser, $this->unparsed_entity_decl_handler(...));
xml_parse($parser, $data, true);
xml_parser_free($parser);
}
Expand Down
7 changes: 6 additions & 1 deletion ext/xml/tests/xml_set_object_multiple_times.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ xml_parse($parser, <<<XML
XML);

?>
--EXPECT--
--EXPECTF--
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d

Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d

Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
A::start_element(CONTAINER)
B::start_element(CHILD)
end_handler(CHILD)
Expand Down
7 changes: 6 additions & 1 deletion ext/xml/tests/xml_set_object_multiple_times_errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ xml_parse($parser, <<<XML
XML);

?>
--EXPECT--
--EXPECTF--
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d

Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
A::start_element(CONTAINER)

Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
ValueError: xml_set_object(): Argument #2 ($object) cannot safely swap to object of class B as method "end_element" does not exist, which was set via xml_set_element_handler()
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class XML_Parser
function parse($data)
{
$parser = xml_parser_create();
xml_set_object($parser, $this);
xml_set_processing_instruction_handler($parser, "PIHandler");
xml_set_processing_instruction_handler($parser, $this->PIHandler(...));
xml_parse($parser, $data, true);
xml_parser_free($parser);
}
Expand Down
25 changes: 24 additions & 1 deletion ext/xml/xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "php.h"

#include "zend_variables.h"
#include "zend_attributes.h"
#include "ext/standard/info.h"
#include "ext/standard/html.h" /* For php_next_utf8_char() */

Expand Down Expand Up @@ -1184,6 +1185,13 @@ PHP_FUNCTION(xml_set_element_handler)
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OF!S", &pind, xml_parser_ce, &start_fci, &start_fcc, &end_method_name) == SUCCESS) {
parser = Z_XMLPARSER_P(pind);

php_error_docref(NULL, E_DEPRECATED, "Passing non-callable strings is deprecated since 8.4");
if (UNEXPECTED(EG(exception))) {
zend_release_fcall_info_cache(&start_fcc);
zend_release_fcall_info_cache(&end_fcc);
RETURN_THROWS();
}

bool status = php_xml_check_string_method_arg(3, parser->object, end_method_name, &end_fcc);
if (status == false) {
zend_release_fcall_info_cache(&start_fcc);
Expand All @@ -1193,6 +1201,13 @@ PHP_FUNCTION(xml_set_element_handler)
} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OSF!", &pind, xml_parser_ce, &start_method_name, &end_fci, &end_fcc) == SUCCESS) {
parser = Z_XMLPARSER_P(pind);

php_error_docref(NULL, E_DEPRECATED, "Passing non-callable strings is deprecated since 8.4");
if (UNEXPECTED(EG(exception))) {
zend_release_fcall_info_cache(&start_fcc);
zend_release_fcall_info_cache(&end_fcc);
RETURN_THROWS();
}

bool status = php_xml_check_string_method_arg(2, parser->object, start_method_name, &start_fcc);
if (status == false) {
zend_release_fcall_info_cache(&start_fcc);
Expand All @@ -1203,6 +1218,11 @@ PHP_FUNCTION(xml_set_element_handler)
zend_release_fcall_info_cache(&start_fcc);
zend_release_fcall_info_cache(&end_fcc);

php_error_docref(NULL, E_DEPRECATED, "Passing non-callable strings is deprecated since 8.4");
if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}

parser = Z_XMLPARSER_P(pind);

bool status = php_xml_check_string_method_arg(2, parser->object, start_method_name, &start_fcc);
Expand Down Expand Up @@ -1263,7 +1283,10 @@ static void php_xml_set_handler_parse_callable(
memcpy(parser_handler_fcc, &handler_fcc, sizeof(zend_fcall_info_cache));
} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OS", &pind, xml_parser_ce, &method_name) == SUCCESS) {
*parser = Z_XMLPARSER_P(pind);

php_error_docref(NULL, E_DEPRECATED, "Passing non-callable strings is deprecated since 8.4");
if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}
bool status = php_xml_check_string_method_arg(2, (*parser)->object, method_name, parser_handler_fcc);
if (status == false) {
RETURN_THROWS();
Expand Down
1 change: 1 addition & 0 deletions ext/xml/xml.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ function xml_parser_create(?string $encoding = null): XMLParser {}

function xml_parser_create_ns(?string $encoding = null, string $separator = ":"): XMLParser {}

#[\Deprecated(since: '8.4', message: 'provide a proper method callable to xml_set_*_handler() functions')]
function xml_set_object(XMLParser $parser, object $object): true {}

function xml_set_element_handler(XMLParser $parser, callable|string|null $start_handler, callable|string|null $end_handler): true {}
Expand Down
17 changes: 15 additions & 2 deletions ext/xml/xml_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading