]> granicus.if.org Git - php/commitdiff
Add error handling for element/attribute creation/changing
authorMarcus Boerger <helly@php.net>
Tue, 16 Dec 2003 20:34:19 +0000 (20:34 +0000)
committerMarcus Boerger <helly@php.net>
Tue, 16 Dec 2003 20:34:19 +0000 (20:34 +0000)
ext/simplexml/simplexml.c
ext/simplexml/tests/012.phpt [new file with mode: 0755]

index b753edc9c52b51b661a48dcb3a8c0418eb3d9e8b..852341158517b77097e83db211b6a11c73bac3a7 100644 (file)
@@ -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 (executable)
index 0000000..67e0bca
--- /dev/null
@@ -0,0 +1,40 @@
+--TEST--
+SimpleXML and Attribute creation
+--SKIPIF--
+<?php 
+       if (!extension_loaded('simplexml')) print 'skip';
+       if (!class_exists('RecursiveIteratorIterator')) print 'skip RecursiveIteratorIterator not available';
+?>
+--FILE--
+<?php 
+
+$xml =<<<EOF
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<foo/>
+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
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<foo attr="value"/>
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<foo attr="new value"/>
+
+Fatal error: main(): Cannot create unnamed attribute in %s012.php on line %d