Boost logo

Boost Users :

Subject: [Boost-users] [serialization] Deserialization of a float whose value is infinite throws an archive_exception ("stream error")
From: Luca (lcappa_at_[hidden])
Date: 2010-10-12 10:31:38


Hello,

using the Boost.Serialization library version 1.43, I saved to an XML
archive a float number which has a value of infinite; then loading
that XML file leads to a boost::archive::archive_exception,
exception.what() return "stream error". The source code to reproduce
the problem is the following:

#include <boost/serialization/serialization.hpp>
#include <sstream>
#include <boost/archive/xml_woarchive.hpp>
#include <boost/archive/xml_wiarchive.hpp>
#include <iostream>

struct XXX
{
  friend class boost::serialization::access;
    template<class Archive> void serialize(Archive & ar, const
unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(mValue);
    }
  float mValue;
};//struct

int main()
{
  std::wstringstream lSS;
  boost::archive::xml_woarchive oa(lSS);
  XXX xxx;
  float zero = 0.f;
  xxx.mValue = 1 / zero;
  oa << BOOST_SERIALIZATION_NVP(xxx);
  std::wcout<<"saved:"<<lSS.str() << std::endl;

  boost::archive::xml_wiarchive ia(lSS);
  ia >> BOOST_SERIALIZATION_NVP(xxx);
  std::wcout<<"loaded:"<<lSS.str() << std::endl;
  return 0;
}

Using gcc 4.4.4 on Linux/Debian the mValue member is saved as:

<mValue>inf</mValue>

while on Visual Studio 2005 on Windows XP sp2 the mValue member is saved as:

<mValue>1.#INF</mValue>

I see that the value of infinite has a different representation, (and
this could be the main motivation behind the choice to not support
them), but this lead to a situation in which a properly saved archive
is not loadable by the library itself.

As far I can see, the issue is at this line 209 of
\libs\serialization\src\basic_xml_grammar.ipp

    parse_info<BOOST_DEDUCED_TYPENAME std::basic_string<CharType>::iterator>
        result = boost::spirit::parse(arg.begin(), arg.end(), rule_);
    return result.hit;

where the parse function fails since result.hit is equal to false.

I resolved the issue by checking all the data preventing accurately
any infinite value before it is saved to an xml archive, but I wonder
if this problem belongs to the library itself or not.

Greetings,
Luca


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net