Skip to content

Improve error messages for XSLTProcessor::transformToDoc() #12332

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 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions ext/xsl/tests/transformToDoc_class_exceptions.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
XSLTProcessor::transformToDoc class exceptions
--EXTENSIONS--
xsl
simplexml
dom
--FILE--
<?php

$sxe = simplexml_load_file(__DIR__ . '/53965/collection.xml');

$processor = new XSLTProcessor;
$dom = new DOMDocument;
$dom->load(__DIR__ . '/53965/collection.xsl');
$processor->importStylesheet($dom);

try {
$processor->transformToDoc($sxe, NonExistent::class);
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

try {
$processor->transformToDoc($sxe, DOMDocument::class);
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
XSLTProcessor::transformToDoc(): Argument #2 ($returnClass) must be a valid class name or null, NonExistent given
XSLTProcessor::transformToDoc(): Argument #2 ($returnClass) must be a class name compatible with SimpleXMLElement, DOMDocument given
16 changes: 7 additions & 9 deletions ext/xsl/xsltprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,14 +548,14 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
zval *id, *docp = NULL;
xmlDoc *newdocp;
xsltStylesheetPtr sheetp;
zend_string *ret_class = NULL;
zend_class_entry *ret_class = NULL;
xsl_object *intern;

id = ZEND_THIS;
intern = Z_XSL_P(id);
sheetp = (xsltStylesheetPtr) intern->ptr;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|S!", &docp, &ret_class) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|C!", &docp, &ret_class) == FAILURE) {
RETURN_THROWS();
}

Expand All @@ -564,7 +564,7 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
if (newdocp) {
if (ret_class) {
zend_string *curclass_name;
zend_class_entry *curce, *ce;
zend_class_entry *curce;
php_libxml_node_object *interndoc;

curce = Z_OBJCE_P(docp);
Expand All @@ -573,16 +573,15 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
curce = curce->parent;
}

ce = zend_lookup_class(ret_class);
if (ce == NULL || !instanceof_function(ce, curce)) {
if (!instanceof_function(ret_class, curce)) {
xmlFreeDoc(newdocp);
zend_argument_type_error(2, "must be a class name compatible with %s, \"%s\" given",
ZSTR_VAL(curclass_name), ZSTR_VAL(ret_class)
zend_argument_type_error(2, "must be a class name compatible with %s, %s given",
ZSTR_VAL(curclass_name), ZSTR_VAL(ret_class->name)
);
RETURN_THROWS();
}

object_init_ex(return_value, ce);
object_init_ex(return_value, ret_class);

interndoc = Z_LIBXML_NODE_P(return_value);
php_libxml_increment_doc_ref(interndoc, newdocp);
Expand All @@ -593,7 +592,6 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
} else {
RETURN_FALSE;
}

}
/* }}} end XSLTProcessor::transformToDoc */

Expand Down