helly Tue Dec 16 15:34:20 2003 EDT Added files: /php-src/ext/simplexml/tests 012.phpt
Modified files: /php-src/ext/simplexml simplexml.c Log: Add error handling for element/attribute creation/changing Index: php-src/ext/simplexml/simplexml.c diff -u php-src/ext/simplexml/simplexml.c:1.92 php-src/ext/simplexml/simplexml.c:1.93 --- php-src/ext/simplexml/simplexml.c:1.92 Tue Dec 16 12:14:05 2003 +++ php-src/ext/simplexml/simplexml.c Tue Dec 16 15:34:18 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: simplexml.c,v 1.92 2003/12/16 17:14:05 iliaa Exp $ */ +/* $Id: simplexml.c,v 1.93 2003/12/16 20:34:18 helly Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -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 @@ 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); @@ -1567,7 +1587,7 @@ { php_info_print_table_start(); php_info_print_table_header(2, "Simplexml support", "enabled"); - php_info_print_table_row(2, "Revision", "$Revision: 1.92 $"); + php_info_print_table_row(2, "Revision", "$Revision: 1.93 $"); php_info_print_table_row(2, "Schema support", #ifdef LIBXML_SCHEMAS_ENABLED "enabled"); Index: php-src/ext/simplexml/tests/012.phpt +++ php-src/ext/simplexml/tests/012.phpt --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 -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php