Boost C++ Libraries: Ticket #4809: deserialization with xml_iarchive fails when attributes of top level element are reordered https://svn.boost.org/trac10/ticket/4809 <p> Citing from <a class="ext-link" href="http://www.w3.org/TR/2008/REC-xml-20081126/"><span class="icon">​</span>W3C XML spec</a>, section 3.1: "Note that the order of attribute specifications in a start-tag or empty-element tag is not significant." </p> <p> The parser used by xml_iarchive doesn't conform to this, because it fails on XML input which is logically equivalent to the output of xml_oarchive, just because the attribute order of the top level element 'boost_serialization' was changed. In contrast, the parser seems to be robust against reordering attributes in other elements. </p> <p> The following program demonstrates the issue: </p> <pre class="wiki"> #include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt; #include &lt;boost/archive/xml_iarchive.hpp&gt; void f(const std::string&amp; xml) { try { std::string str; boost::archive::xml_iarchive(std::istringstream(xml)) &gt;&gt; boost::serialization::make_nvp("str", str); std::cout &lt;&lt; str &lt;&lt; std::endl; } catch(boost::archive::archive_exception&amp; e) { std::cout &lt;&lt; e.what() &lt;&lt; std::endl; } } int main() { // deserialize some xml, which was created using xml_oarchive f("&lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?&gt;" "&lt;!DOCTYPE boost_serialization&gt;" "&lt;boost_serialization signature=\"serialization::archive\" version=\"7\"&gt;" "&lt;str&gt;deserialize successfull&lt;/str&gt;" "&lt;/boost_serialization&gt;"); // the same xml, but attributes of &lt;boost_serialization&gt; reorderd f("&lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?&gt;" "&lt;!DOCTYPE boost_serialization&gt;" "&lt;boost_serialization version=\"7\" signature=\"serialization::archive\"&gt;" "&lt;str&gt;deserialize successfull&lt;/str&gt;" "&lt;/boost_serialization&gt;"); return 0; } </pre><p> The following patch to basic_xml_grammar.ipp modifies the rule '<a class="missing wiki">SerializationWrapper</a>' to accept both possible combinations: </p> <pre class="wiki">Index: libs/serialization/src/basic_xml_grammar.ipp =================================================================== --- libs/serialization/src/basic_xml_grammar.ipp (revision 66354) +++ libs/serialization/src/basic_xml_grammar.ipp (working copy) @@ -271,9 +271,9 @@ = -S &gt;&gt; "&lt;boost_serialization" &gt;&gt; S - &gt;&gt; SignatureAttribute - &gt;&gt; S - &gt;&gt; VersionAttribute + &gt;&gt; ( (SignatureAttribute &gt;&gt; S &gt;&gt; VersionAttribute) + | (VersionAttribute &gt;&gt; S &gt;&gt; SignatureAttribute) + ) &gt;&gt; !S &gt;&gt; '&gt;' ; </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4809 Trac 1.4.3 Kolja Nowak <kolja@…> Tue, 02 Nov 2010 15:49:02 GMT attachment set https://svn.boost.org/trac10/ticket/4809 https://svn.boost.org/trac10/ticket/4809 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost-serialization-xml-attr-order-bug.cpp</span> </li> </ul> <p> program to reproduce the issue </p> Ticket Kolja Nowak <kolja@…> Tue, 02 Nov 2010 15:49:25 GMT attachment set https://svn.boost.org/trac10/ticket/4809 https://svn.boost.org/trac10/ticket/4809 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost-serialization-xml-attr-order.patch</span> </li> </ul> <p> patch fixing the issue </p> Ticket Kolja Nowak <kolja@…> Wed, 03 Nov 2010 11:16:50 GMT component changed; owner set https://svn.boost.org/trac10/ticket/4809#comment:1 https://svn.boost.org/trac10/ticket/4809#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Robert Ramey</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">serialization</span> </li> </ul> Ticket Bryce Adelstein Lelbach Wed, 03 Nov 2010 11:48:05 GMT owner changed https://svn.boost.org/trac10/ticket/4809#comment:2 https://svn.boost.org/trac10/ticket/4809#comment:2 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Robert Ramey</span> to <span class="trac-author">Bryce Adelstein Lelbach</span> </li> </ul> <p> Ramey, what are your thoughts here? This adds more complexity to the grammar, and I'm having trouble imagining a situation in which this is needed. This happens with the old grammar, too, I should note. </p> Ticket Kolja Nowak <kolja@…> Wed, 03 Nov 2010 12:14:08 GMT <link>https://svn.boost.org/trac10/ticket/4809#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4809#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4809#comment:2" title="Comment 2">wash</a>: </p> <blockquote class="citation"> <p> I'm having trouble imagining a situation in which this is needed. </p> </blockquote> <p> We use boost::serialize to exchange messages between different processes, and sadly one of them is not written in C++ but in Adobe Flex. We wrote a counterpart to boost::serialize in Flex on top of the XML classes provided by the framework. This worked well until the recent update of Adobe Air, which changed the behavior of the XML generator, causing attributes to be ordered slightly different than before. </p> <p> You may argue boost::serialize was never designed to talk to a different implementation expect itself. But even then, violating XML in the current way may lead to trouble. Imagine I have some storage engine, a database, whatever, which is able to store XML documents in a more space efficient way than plain text, for example by converting them to a binary representation. It would be perfectly fine and XML conformant for such a storage engine to reconstruct logically identical documents with changed attribute order, but even if it looks like a good idea I couldn't use it store boost::serialize XML archives. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Bryce Adelstein Lelbach</dc:creator> <pubDate>Wed, 03 Nov 2010 12:17:16 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4809#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4809#comment:4</guid> <description> <p> Alright, Kolja. I'm sold. Unless Ramey disagrees, I think we should apply this against the old version of the grammar which will be in trunk soon, and I've already applied it to my local trunk. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Bryce Adelstein Lelbach</dc:creator> <pubDate>Mon, 08 Nov 2010 17:13:05 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4809#comment:5 https://svn.boost.org/trac10/ticket/4809#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Applied in <a class="changeset" href="https://svn.boost.org/trac10/changeset/66459" title="Fix for https://svn.boost.org/trac/boost/ticket/4809; fixed includes ...">r66459</a>. </p> Ticket