From f88368cfc81d817c36650e58771940d292e2a8b8 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 2 Oct 2023 19:21:54 +0200 Subject: [PATCH] Improve error messages for XSLTProcessor::transformToDoc() (#12332) * Improve error messages for XSLTProcessor::transformToDoc() Also adds a relevant test file, as these branches were untested before. --- .../transformToDoc_class_exceptions.phpt | 32 +++++++++++++++++++ ext/xsl/xsltprocessor.c | 16 ++++------ 2 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 ext/xsl/tests/transformToDoc_class_exceptions.phpt diff --git a/ext/xsl/tests/transformToDoc_class_exceptions.phpt b/ext/xsl/tests/transformToDoc_class_exceptions.phpt new file mode 100644 index 00000000000..566d8711baa --- /dev/null +++ b/ext/xsl/tests/transformToDoc_class_exceptions.phpt @@ -0,0 +1,32 @@ +--TEST-- +XSLTProcessor::transformToDoc class exceptions +--EXTENSIONS-- +xsl +simplexml +dom +--FILE-- +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 diff --git a/ext/xsl/xsltprocessor.c b/ext/xsl/xsltprocessor.c index fb607c40b1a..23e431d5e37 100644 --- a/ext/xsl/xsltprocessor.c +++ b/ext/xsl/xsltprocessor.c @@ -497,14 +497,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(); } @@ -513,7 +513,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); @@ -522,16 +522,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); @@ -542,7 +541,6 @@ PHP_METHOD(XSLTProcessor, transformToDoc) } else { RETURN_FALSE; } - } /* }}} end XSLTProcessor::transformToDoc */