Skip to content

Commit 2f601d8

Browse files
committed
Promote warnings in ext/xsl
1 parent 1efbc2c commit 2f601d8

3 files changed

+46
-27
lines changed

ext/xsl/tests/xsltprocessor_importStylesheet-invalidparam.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ if (!extension_loaded('xsl')) {
1111

1212
$xslt = new XSLTProcessor();
1313
$dummy = new stdClass();
14-
var_dump($xslt->importStylesheet($dummy));
14+
try {
15+
var_dump($xslt->importStylesheet($dummy));
16+
} catch (ValueError $e) {
17+
echo $e->getMessage(), "\n";
18+
}
1519

1620
?>
17-
--EXPECTF--
18-
Warning: Invalid Document in %s on line %d
19-
bool(false)
21+
--EXPECT--
22+
XSLTProcessor::importStylesheet(): Argument #1 ($stylesheet) must be a valid XML node
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Calling XSLTProcessor::transformToDoc() without stylesheet
3+
--FILE--
4+
<?php
5+
6+
$doc = new DOMDocument('1.0', 'utf-8');
7+
8+
$xsl = new XSLTProcessor;
9+
try {
10+
$xsl->transformToDoc($doc);
11+
} catch (Error $e) {
12+
echo $e->getMessage(), "\n";
13+
}
14+
15+
?>
16+
--EXPECT--
17+
XSLTProcessor::transformToDoc() can only be called after a stylesheet has been imported

ext/xsl/xsltprocessor.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,22 @@ static char **php_xsl_xslt_make_params(HashTable *parht, int xpath_params)
6666
memset((char *)params, 0, parsize);
6767

6868
ZEND_HASH_FOREACH_STR_KEY_VAL(parht, string_key, value) {
69-
if (string_key == NULL) {
70-
php_error_docref(NULL, E_WARNING, "Invalid argument or parameter array");
71-
efree(params);
72-
return NULL;
73-
} else {
74-
if (Z_TYPE_P(value) != IS_STRING) {
75-
if (!try_convert_to_string(value)) {
76-
efree(params);
77-
return NULL;
78-
}
69+
ZEND_ASSERT(string_key != NULL);
70+
if (Z_TYPE_P(value) != IS_STRING) {
71+
if (!try_convert_to_string(value)) {
72+
efree(params);
73+
return NULL;
7974
}
75+
}
8076

81-
if (!xpath_params) {
82-
xpath_expr = php_xsl_xslt_string_to_xpathexpr(Z_STRVAL_P(value));
83-
} else {
84-
xpath_expr = estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value));
85-
}
86-
if (xpath_expr) {
87-
params[i++] = estrndup(ZSTR_VAL(string_key), ZSTR_LEN(string_key));
88-
params[i++] = xpath_expr;
89-
}
77+
if (!xpath_params) {
78+
xpath_expr = php_xsl_xslt_string_to_xpathexpr(Z_STRVAL_P(value));
79+
} else {
80+
xpath_expr = estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value));
81+
}
82+
if (xpath_expr) {
83+
params[i++] = estrndup(ZSTR_VAL(string_key), ZSTR_LEN(string_key));
84+
params[i++] = xpath_expr;
9085
}
9186
} ZEND_HASH_FOREACH_END();
9287

@@ -336,8 +331,8 @@ PHP_METHOD(XSLTProcessor, importStylesheet)
336331
doc = nodep->doc;
337332
}
338333
if (doc == NULL) {
339-
php_error(E_WARNING, "Invalid Document");
340-
RETURN_FALSE;
334+
zend_argument_value_error(1, "must be a valid XML node");
335+
RETURN_THROWS();
341336
}
342337

343338
/* libxslt uses _private, so we must copy the imported
@@ -417,13 +412,17 @@ static xmlDocPtr php_xsl_apply_stylesheet(zval *id, xsl_object *intern, xsltStyl
417412
if (node) {
418413
doc = node->doc;
419414
}
415+
420416
if (doc == NULL) {
421-
php_error_docref(NULL, E_WARNING, "Invalid Document");
417+
zend_argument_value_error(1, "must be a valid XML node");
422418
return NULL;
423419
}
424420

425421
if (style == NULL) {
426-
php_error_docref(NULL, E_WARNING, "No stylesheet associated to this object");
422+
zend_string *name = get_active_function_or_method_name();
423+
zend_throw_error(NULL, "%s() can only be called after a stylesheet has been imported",
424+
ZSTR_VAL(name));
425+
zend_string_release(name);
427426
return NULL;
428427
}
429428

0 commit comments

Comments
 (0)