Skip to content

Commit e050b24

Browse files
committed
Promote warnings to exceptions in ext/simplexml
1 parent 4514afc commit e050b24

6 files changed

+63
-41
lines changed

ext/simplexml/simplexml.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ static void change_node_zval(xmlNodePtr node, zval *value)
400400
}
401401
break;
402402
default:
403-
php_error_docref(NULL, E_WARNING, "It is not possible to assign complex types to nodes");
403+
zend_type_error("It's not possible to assign a complex type to nodes, %s given", zend_zval_type_name(value));
404404
break;
405405
}
406406
}
@@ -457,7 +457,7 @@ static zval *sxe_prop_dim_write(zend_object *object, zval *member, zval *value,
457457
}
458458

459459
if (!Z_STRLEN_P(member)) {
460-
php_error_docref(NULL, E_WARNING, "Cannot write or create unnamed %s", attribs ? "attribute" : "element");
460+
zend_value_error("Cannot create or write %s with an empty name", attribs ? "attributes" : "elements");
461461
if (member == &tmp_zv) {
462462
zval_ptr_dtor_str(&tmp_zv);
463463
}
@@ -485,7 +485,7 @@ static zval *sxe_prop_dim_write(zend_object *object, zval *member, zval *value,
485485
* and could also be E_PARSE, but we use this only during parsing
486486
* and this is during runtime.
487487
*/
488-
zend_throw_error(NULL, "Cannot create unnamed attribute");
488+
zend_value_error("Cannot create or write attributes with an empty name");
489489
return &EG(error_zval);
490490
}
491491
if (attribs && !node && sxe->iter.type == SXE_ITER_ELEMENT) {
@@ -527,7 +527,7 @@ static zval *sxe_prop_dim_write(zend_object *object, zval *member, zval *value,
527527
if (member == &tmp_zv) {
528528
zval_ptr_dtor_str(&tmp_zv);
529529
}
530-
zend_error(E_WARNING, "It is not yet possible to assign complex types to %s", attribs ? "attributes" : "properties");
530+
zend_type_error("It's not possible to assign a complex type to %s, %s given", attribs ? "attributes" : "properties", zend_zval_type_name(value));
531531
return &EG(error_zval);
532532
}
533533
}
@@ -1710,8 +1710,8 @@ SXE_METHOD(addChild)
17101710
}
17111711

17121712
if (qname_len == 0) {
1713-
php_error_docref(NULL, E_WARNING, "Element name is required");
1714-
return;
1713+
zend_argument_value_error(1, "cannot be empty");
1714+
RETURN_THROWS();
17151715
}
17161716

17171717
sxe = Z_SXEOBJ_P(ZEND_THIS);
@@ -1775,8 +1775,8 @@ SXE_METHOD(addAttribute)
17751775
}
17761776

17771777
if (qname_len == 0) {
1778-
php_error_docref(NULL, E_WARNING, "Attribute name is required");
1779-
return;
1778+
zend_argument_value_error(1, "cannot be empty");
1779+
RETURN_THROWS();
17801780
}
17811781

17821782
sxe = Z_SXEOBJ_P(ZEND_THIS);
@@ -2300,8 +2300,8 @@ PHP_FUNCTION(simplexml_load_file)
23002300
}
23012301

23022302
if (ZEND_LONG_EXCEEDS_INT(options)) {
2303-
php_error_docref(NULL, E_WARNING, "Invalid options");
2304-
RETURN_FALSE;
2303+
zend_argument_value_error(3, "is too large");
2304+
RETURN_THROWS();
23052305
}
23062306

23072307
docp = xmlReadFile(filename, NULL, (int)options);
@@ -2345,16 +2345,16 @@ PHP_FUNCTION(simplexml_load_string)
23452345
}
23462346

23472347
if (ZEND_SIZE_T_INT_OVFL(data_len)) {
2348-
php_error_docref(NULL, E_WARNING, "Data is too long");
2349-
RETURN_FALSE;
2348+
zend_argument_value_error(1, "is too long");
2349+
RETURN_THROWS();
23502350
}
23512351
if (ZEND_SIZE_T_INT_OVFL(ns_len)) {
2352-
php_error_docref(NULL, E_WARNING, "Namespace is too long");
2353-
RETURN_FALSE;
2352+
zend_argument_value_error(4, "is too long");
2353+
RETURN_THROWS();
23542354
}
23552355
if (ZEND_LONG_EXCEEDS_INT(options)) {
2356-
php_error_docref(NULL, E_WARNING, "Invalid options");
2357-
RETURN_FALSE;
2356+
zend_argument_value_error(3, "is too large");
2357+
RETURN_THROWS();
23582358
}
23592359

23602360
docp = xmlReadMemory(data, (int)data_len, NULL, NULL, (int)options);

ext/simplexml/tests/012.phpt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ EOF;
1414

1515
$sxe = simplexml_load_string($xml);
1616

17+
try {
18+
$sxe[""] = "warning";
19+
} catch (ValueError $exception) {
20+
echo $exception->getMessage() . "\n";
21+
}
1722

18-
$sxe[""] = "warning";
1923
$sxe["attr"] = "value";
2024

2125
echo $sxe->asXML();
@@ -24,19 +28,19 @@ $sxe["attr"] = "new value";
2428

2529
echo $sxe->asXML();
2630

27-
$sxe[] = "error";
31+
try {
32+
$sxe[] = "error";
33+
} catch (ValueError $exception) {
34+
echo $exception->getMessage() . "\n";
35+
}
2836

2937
__HALT_COMPILER();
3038
?>
3139
===DONE===
32-
--EXPECTF--
33-
Warning: main(): Cannot write or create unnamed attribute in %s012.php on line %d
40+
--EXPECT--
41+
Cannot create or write attributes with an empty name
3442
<?xml version="1.0" encoding="ISO-8859-1"?>
3543
<foo attr="value"/>
3644
<?xml version="1.0" encoding="ISO-8859-1"?>
3745
<foo attr="new value"/>
38-
39-
Fatal error: Uncaught Error: Cannot create unnamed attribute in %s012.php:%d
40-
Stack trace:
41-
#0 {main}
42-
thrown in %s012.php on line %d
46+
Cannot create or write attributes with an empty name

ext/simplexml/tests/SimpleXMLElement_addAttribute_required_attribute_name.phpt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ Havard Eide <[email protected]>
88
--FILE--
99
<?php
1010
$a = new SimpleXMLElement("<php>testfest</php>");
11-
$a->addAttribute( "", "" );
11+
12+
try {
13+
$a->addAttribute( "", "" );
14+
} catch (ValueError $exception) {
15+
echo $exception->getMessage() . "\n";
16+
}
17+
1218
echo $a->asXML();
1319
?>
14-
--EXPECTF--
15-
Warning: SimpleXMLElement::addAttribute(): Attribute name is required in %s on line %d
20+
--EXPECT--
21+
SimpleXMLElement::addAttribute(): Argument #1 ($qualifiedName) cannot be empty
1622
<?xml version="1.0"?>
1723
<php>testfest</php>

ext/simplexml/tests/SimpleXMLElement_xpath_4.phpt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platforms only");
77
?>
88
--FILE--
99
<?php
10-
$xml = simplexml_load_string("XXXXXXX^",$x,0x6000000000000001);
11-
var_dump($xml);
10+
11+
try {
12+
simplexml_load_string("XXXXXXX^", $x, 0x6000000000000001);
13+
} catch (ValueError $exception) {
14+
echo $exception->getMessage() . "\n";
15+
}
16+
1217
?>
1318
--EXPECTF--
1419
Warning: Undefined variable $x in %s on line %d
15-
16-
Warning: simplexml_load_string(): Invalid options in %s on line %d
17-
bool(false)
20+
simplexml_load_string(): Argument #3 ($options) is too large

ext/simplexml/tests/bug37076_1.phpt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ Bug #37076 (SimpleXML ignores .=) (appending to unnamed attribute)
44
<?php if (!extension_loaded("simplexml")) print "skip"; ?>
55
--FILE--
66
<?php
7+
78
$xml = simplexml_load_string("<root><foo /></root>");
8-
$xml->{""} .= "bar";
9+
10+
try {
11+
$xml->{""} .= "bar";
12+
} catch (ValueError $exception) {
13+
echo $exception->getMessage() . "\n";
14+
}
15+
916
print $xml->asXML();
1017
?>
11-
--EXPECTF--
12-
Warning: main(): Cannot write or create unnamed element in %s on line %d
13-
14-
Warning: main(): Cannot write or create unnamed element in %s on line %d
18+
--EXPECT--
19+
Cannot create or write elements with an empty name
1520
<?xml version="1.0"?>
1621
<root><foo/></root>

ext/simplexml/tests/bug38406.phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ $item->otherAttribute = $item->attribute;
1313
var_dump($item->otherAttribute);
1414

1515
$a = array();
16-
$item->$a = new stdclass;
16+
17+
try {
18+
$item->$a = new stdclass;
19+
} catch (TypeError $exception) {
20+
echo $exception->getMessage() . "\n";
21+
}
1722

1823
echo "Done\n";
1924
?>
@@ -28,6 +33,5 @@ object(SimpleXMLElement)#%d (1) {
2833
}
2934

3035
Warning: Array to string conversion in %s on line %d
31-
32-
Warning: It is not yet possible to assign complex types to properties in %s on line %d
36+
It's not possible to assign a complex type to properties, stdClass given
3337
Done

0 commit comments

Comments
 (0)