From: Marcus Boerger Date: Tue, 16 Dec 2003 20:34:19 +0000 (+0000) Subject: Add error handling for element/attribute creation/changing X-Git-Tag: php-5.0.0b3RC1~28 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4dd4bcdbb50d9302ba5b11b9a4ead4fc6bddc023;p=php Add error handling for element/attribute creation/changing --- diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c index b753edc9c5..8523411585 100644 --- a/ext/simplexml/simplexml.c +++ b/ext/simplexml/simplexml.c @@ -28,6 +28,7 @@ #include "php_ini.h" #include "ext/standard/info.h" +#include "ext/standard/php_string.h" #include "php_simplexml.h" #if HAVE_SPL @@ -279,13 +280,32 @@ static void sxe_prop_dim_write(zval *object, zval *member, zval *value, zend_boo xmlAttrPtr attr = NULL; int counter = 0; int is_attr = 0; - zval tmp_zv; + zval tmp_zv, trim_zv; + + if (!member) { + /* this happens when the user did: $sxe[] = $value + * and could also be E_PARSE, but use this only during parsing + * but this is during runtime. + */ + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Cannot create unnamed attribute"); + return; + } if (Z_TYPE_P(member) != IS_STRING) { - tmp_zv = *member; - zval_copy_ctor(&tmp_zv); + trim_zv = *member; + zval_copy_ctor(&trim_zv); + convert_to_string(&trim_zv); + php_trim(Z_STRVAL(trim_zv), Z_STRLEN(trim_zv), NULL, 0, &tmp_zv, 3 TSRMLS_CC); + zval_dtor(&trim_zv); member = &tmp_zv; - convert_to_string(member); + } + + if (!Z_STRLEN_P(member)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot write or create unnamed %s", attribs ? "attribute" : "element"); + if (member == &tmp_zv) { + zval_dtor(&tmp_zv); + } + return; } name = Z_STRVAL_P(member); diff --git a/ext/simplexml/tests/012.phpt b/ext/simplexml/tests/012.phpt new file mode 100755 index 0000000000..67e0bca346 --- /dev/null +++ b/ext/simplexml/tests/012.phpt @@ -0,0 +1,40 @@ +--TEST-- +SimpleXML and Attribute creation +--SKIPIF-- + +--FILE-- + + +EOF; + +$sxe = simplexml_load_string($xml); + + +$sxe[""] = "warning"; +$sxe["attr"] = "value"; + +echo $sxe->to_xml_string(); + +$sxe["attr"] = "new value"; + +echo $sxe->to_xml_string(); + +$sxe[] = "error"; + +?> +===DONE=== +--EXPECTF-- + +Warning: main(): Cannot write or create unnamed attribute in %s012.php on line %d + + + + + +Fatal error: main(): Cannot create unnamed attribute in %s012.php on line %d