Skip to content

Commit 8b77a64

Browse files
committed
ext/xsl: Remove dead code in importStylesheet and use direct property slot access for doXInclude and cloneDocument.
The node and its document are validated before cloning, making the subsequent NULL checks on nodep and newdoc redundant. Use OBJ_PROP_NUM via XSL_DEFINE_PROP_ACCESSOR for doXInclude and cloneDocument, avoiding zend_string allocation and property hash lookup per call.
1 parent 1655d57 commit 8b77a64

4 files changed

Lines changed: 18 additions & 31 deletions

File tree

ext/xsl/php_xsl.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ zend_object *xsl_objects_new(zend_class_entry *class_type)
144144
}
145145
#endif
146146

147+
XSL_DEFINE_PROP_ACCESSOR(do_xinclude, "doXInclude", 0)
148+
XSL_DEFINE_PROP_ACCESSOR(clone_document, "cloneDocument", 1)
147149
XSL_DEFINE_PROP_ACCESSOR(max_template_depth, "maxTemplateDepth", 2)
148150
XSL_DEFINE_PROP_ACCESSOR(max_template_vars, "maxTemplateVars", 3)
149151

ext/xsl/php_xsl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ void xsl_ext_function_object_php(xmlXPathParserContextPtr ctxt, int nargs);
7979

8080
zval *xsl_prop_max_template_depth(zend_object *object);
8181
zval *xsl_prop_max_template_vars(zend_object *object);
82+
zval *xsl_prop_do_xinclude(zend_object *object);
83+
zval *xsl_prop_clone_document(zend_object *object);
8284

8385
PHP_MINIT_FUNCTION(xsl);
8486
PHP_MSHUTDOWN_FUNCTION(xsl);

ext/xsl/tests/special_operations_with_properties.phpt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,16 @@ echo "--- Unset cloneDocument ---\n";
2929
$xslt = new XSLTProcessor;
3030
$xslt->registerPHPFunctions();
3131
unset($xslt->cloneDocument);
32-
try {
33-
$xslt->importStylesheet($xsl);
34-
} catch (Error $e) {
35-
echo $e->getMessage(), "\n";
36-
}
32+
$xslt->importStylesheet($xsl);
33+
echo $xslt->transformToXml($xml);
3734

3835
echo "--- Unset doXInclude ---\n";
3936

4037
$xslt = new XSLTProcessor;
4138
$xslt->registerPHPFunctions();
4239
unset($xslt->doXInclude);
4340
$xslt->importStylesheet($xsl);
44-
try {
45-
echo $xslt->transformToXml($xml);
46-
} catch (Error $e) {
47-
echo $e->getMessage(), "\n";
48-
}
41+
echo $xslt->transformToXml($xml);
4942

5043
echo "--- Make properties references ---\n";
5144

@@ -59,9 +52,13 @@ echo $xslt->transformToXml($xml);
5952
?>
6053
--EXPECT--
6154
--- Unset cloneDocument ---
62-
Typed property XSLTProcessor::$cloneDocument must not be accessed before initialization
55+
Called test
56+
<?xml version="1.0"?>
57+
hello
6358
--- Unset doXInclude ---
64-
Typed property XSLTProcessor::$doXInclude must not be accessed before initialization
59+
Called test
60+
<?xml version="1.0"?>
61+
hello
6562
--- Make properties references ---
6663
Called test
6764
<?xml version="1.0"?>

ext/xsl/xsltprocessor.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ PHP_METHOD(XSLTProcessor, importStylesheet)
167167
xsltStylesheetPtr sheetp;
168168
bool clone_docu = false;
169169
xmlNode *nodep = NULL;
170-
zval *cloneDocu, rv, clone_zv, owner_zv;
171-
zend_string *member;
170+
zval *cloneDocu, clone_zv, owner_zv;
172171

173172
id = ZEND_THIS;
174173
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &docp) == FAILURE) {
@@ -216,15 +215,7 @@ PHP_METHOD(XSLTProcessor, importStylesheet)
216215
ZVAL_OBJ(&clone_zv, clone);
217216
nodep = php_libxml_import_node(&clone_zv);
218217

219-
if (nodep) {
220-
newdoc = nodep->doc;
221-
}
222-
if (newdoc == NULL) {
223-
OBJ_RELEASE(clone);
224-
zend_argument_type_error(1, "must be a valid XML node");
225-
RETURN_THROWS();
226-
}
227-
218+
newdoc = nodep->doc;
228219
php_libxml_node_object *clone_lxml_obj = Z_LIBXML_NODE_P(&clone_zv);
229220

230221
PHP_LIBXML_SANITIZE_GLOBALS(parse);
@@ -259,10 +250,8 @@ PHP_METHOD(XSLTProcessor, importStylesheet)
259250
intern->sheet_ref_obj->refcount++;
260251
OBJ_RELEASE(clone);
261252

262-
member = ZSTR_INIT_LITERAL("cloneDocument", 0);
263-
cloneDocu = zend_std_read_property(Z_OBJ_P(id), member, BP_VAR_R, NULL, &rv);
253+
cloneDocu = xsl_prop_clone_document(Z_OBJ_P(id));
264254
clone_docu = zend_is_true(cloneDocu);
265-
zend_string_release_ex(member, 0);
266255
if (!clone_docu) {
267256
/* Check if the stylesheet is using xsl:key, if yes, we have to clone the document _always_ before a transformation.
268257
* xsl:key elements may only occur at the top level. Furthermore, all elements at the top level must be in a
@@ -302,8 +291,7 @@ static xmlDocPtr php_xsl_apply_stylesheet(zval *id, xsl_object *intern, xsltStyl
302291
xmlNodePtr node = NULL;
303292
xsltTransformContextPtr ctxt;
304293
php_libxml_node_object *object;
305-
zval *doXInclude, rv;
306-
zend_string *member;
294+
zval *doXInclude;
307295
FILE *f;
308296
int secPrefsError = 0;
309297
xsltSecurityPrefsPtr secPrefs = NULL;
@@ -359,10 +347,8 @@ static xmlDocPtr php_xsl_apply_stylesheet(zval *id, xsl_object *intern, xsltStyl
359347
}
360348
}
361349

362-
member = ZSTR_INIT_LITERAL("doXInclude", 0);
363-
doXInclude = zend_std_read_property(Z_OBJ_P(id), member, BP_VAR_R, NULL, &rv);
350+
doXInclude = xsl_prop_do_xinclude(Z_OBJ_P(id));
364351
ctxt->xinclude = zend_is_true(doXInclude);
365-
zend_string_release_ex(member, 0);
366352

367353
zval *max_template_depth = xsl_prop_max_template_depth(Z_OBJ_P(id));
368354
ZEND_ASSERT(Z_TYPE_P(max_template_depth) == IS_LONG);

0 commit comments

Comments
 (0)