From: <vac...@us...> - 2009-07-25 16:31:08
|
Revision: 160 http://xmlwrapp.svn.sourceforge.net/xmlwrapp/?rev=160&view=rev Author: vaclavslavik Date: 2009-07-25 16:31:00 +0000 (Sat, 25 Jul 2009) Log Message: ----------- Fixed xml::tree_parser to fail on non-fatal parser errors (patch by Sergey Satskiy) Modified Paths: -------------- trunk/AUTHORS trunk/NEWS trunk/src/libxml/tree_parser.cxx trunk/tests/tree/test_tree.cxx Modified: trunk/AUTHORS =================================================================== --- trunk/AUTHORS 2009-07-07 10:00:48 UTC (rev 159) +++ trunk/AUTHORS 2009-07-25 16:31:00 UTC (rev 160) @@ -7,6 +7,7 @@ Tom Browder <tbr...@us...> Other contributors: + Sergey Satskiy <ser...@gm...> Vadim Zeitlin <va...@tt...> Tiziano Mueller <ti...@us...> Greg Chicares <gch...@sb...> Modified: trunk/NEWS =================================================================== --- trunk/NEWS 2009-07-07 10:00:48 UTC (rev 159) +++ trunk/NEWS 2009-07-25 16:31:00 UTC (rev 160) @@ -1,3 +1,6 @@ + + Fixed xml::tree_parser to fail on non-fatal parser errors. + Version 0.6.1 Added Visual C++ 200x projects and fixed VC6 project. Modified: trunk/src/libxml/tree_parser.cxx =================================================================== --- trunk/src/libxml/tree_parser.cxx 2009-07-07 10:00:48 UTC (rev 159) +++ trunk/src/libxml/tree_parser.cxx 2009-07-25 16:31:00 UTC (rev 160) @@ -106,6 +106,8 @@ tree_impl *p = static_cast<tree_impl*>(ctxt->_private); if (!p) return; // handle bug in older versions of libxml + p->okay_ = false; + va_list ap; va_start(ap, message); printf2string(p->last_error_, message, ap); @@ -137,14 +139,24 @@ xml::tree_parser::tree_parser (const char *name, bool allow_exceptions) { std::auto_ptr<tree_impl> ap(pimpl_ = new tree_impl); + pimpl_->okay_ = true; xmlDocPtr tmpdoc = xmlSAXParseFileWithData(&(pimpl_->sax_), name, 0, pimpl_); - if (allow_exceptions && !tmpdoc) throw std::runtime_error(pimpl_->last_error_); - if (tmpdoc) { - pimpl_->doc_.set_doc_data(tmpdoc); - pimpl_->okay_ = true; + if (tmpdoc && pimpl_->okay_) + { + // all is fine + pimpl_->doc_.set_doc_data(tmpdoc); } + else + { + // a problem appeared + if (tmpdoc) + xmlFreeDoc(tmpdoc); + if (allow_exceptions) + throw std::runtime_error(pimpl_->last_error_); + } + ap.release(); } //#################################################################### @@ -161,16 +173,21 @@ ctxt->_private = pimpl_; - xmlParseDocument(ctxt); + pimpl_->okay_ = true; + const int retval = xmlParseDocument(ctxt); - if (!ctxt->wellFormed) { - xmlFreeDoc(ctxt->myDoc); - ctxt->myDoc = 0; - ctxt->sax = 0; - xmlFreeParserCtxt(ctxt); + if (!ctxt->wellFormed || retval != 0 || !pimpl_->okay_) { + xmlFreeDoc(ctxt->myDoc); + ctxt->myDoc = 0; + ctxt->sax = 0; + xmlFreeParserCtxt(ctxt); + pimpl_->okay_ = false; - if (allow_exceptions) throw std::runtime_error(pimpl_->last_error_); - ap.release(); return; // handle non-exception case + if (allow_exceptions) + throw std::runtime_error(pimpl_->last_error_); + + ap.release(); + return; // handle non-exception case } pimpl_->doc_.set_doc_data(ctxt->myDoc); Modified: trunk/tests/tree/test_tree.cxx =================================================================== --- trunk/tests/tree/test_tree.cxx 2009-07-07 10:00:48 UTC (rev 159) +++ trunk/tests/tree/test_tree.cxx 2009-07-25 16:31:00 UTC (rev 160) @@ -186,4 +186,31 @@ } + +namespace +{ + +// this is invalid markup, libxml2 fails with +// namespace error : Namespace prefix a on book is not defined +const std::string XMLDATA_BAD_NS = + "<a:book> xmlns:a=\"a\" <title>title</title></a:book>"; + +} // anonymous namespace + +BOOST_AUTO_TEST_CASE( bad_ns_xml_data_throw ) +{ + + BOOST_CHECK_THROW + ( + xml::tree_parser parser( XMLDATA_BAD_NS.c_str(), XMLDATA_BAD_NS.size()), + std::exception + ); +} + +BOOST_AUTO_TEST_CASE( bad_ns_xml_data_no_throw ) +{ + xml::tree_parser parser(XMLDATA_BAD_NS.c_str(), XMLDATA_BAD_NS.size(), false); + BOOST_CHECK( !parser ); // failed +} + BOOST_AUTO_TEST_SUITE_END() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |