mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
fixes a memory issue with rapidxml, we need to make sure that input streams are null terminated (C++ api yeah right!)
This commit is contained in:
@@ -238,6 +238,7 @@ namespace cereal
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
itsData.push_back('\0'); // rapidxml will do terrible things without this
|
||||||
itsXML.parse<rapidxml::parse_no_data_nodes | rapidxml::parse_declaration_node>( reinterpret_cast<char *>( itsData.data() ) );
|
itsXML.parse<rapidxml::parse_no_data_nodes | rapidxml::parse_declaration_node>( reinterpret_cast<char *>( itsData.data() ) );
|
||||||
}
|
}
|
||||||
catch( rapidxml::parse_error const & e )
|
catch( rapidxml::parse_error const & e )
|
||||||
@@ -273,22 +274,25 @@ namespace cereal
|
|||||||
itsNodes.top().advance();
|
itsNodes.top().advance();
|
||||||
}
|
}
|
||||||
|
|
||||||
// int
|
//! Loads a bool
|
||||||
// long
|
template <class T> inline
|
||||||
// long long
|
typename std::enable_if<std::is_unsigned<T>::value && std::is_same<T, bool>::value, void>::type
|
||||||
// ulong
|
loadValue( T & value )
|
||||||
// ulong long
|
{
|
||||||
//
|
std::istringstream is( itsNodes.top().node->value() );
|
||||||
|
is.setf( std::ios::boolalpha );
|
||||||
|
is >> value;
|
||||||
|
}
|
||||||
|
|
||||||
template <class T> inline
|
template <class T> inline
|
||||||
typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) < sizeof(long long), void>::type
|
typename std::enable_if<std::is_unsigned<T>::value && !std::is_same<T, bool>::value && sizeof(T) < sizeof(long long), void>::type
|
||||||
loadValue( T & value )
|
loadValue( T & value )
|
||||||
{
|
{
|
||||||
value = std::stoul( itsNodes.top().node->value() );
|
value = std::stoul( itsNodes.top().node->value() );
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T> inline
|
template <class T> inline
|
||||||
typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) >= sizeof(long long), void>::type
|
typename std::enable_if<std::is_unsigned<T>::value && !std::is_same<T, bool>::value && sizeof(T) >= sizeof(long long), void>::type
|
||||||
loadValue( T & value )
|
loadValue( T & value )
|
||||||
{
|
{
|
||||||
value = std::stoull( itsNodes.top().node->value() );
|
value = std::stoull( itsNodes.top().node->value() );
|
||||||
@@ -401,7 +405,7 @@ namespace cereal
|
|||||||
}; // NodeInfo
|
}; // NodeInfo
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<uint8_t> itsData; //!< The raw data loaded
|
std::vector<char> itsData; //!< The raw data loaded
|
||||||
rapidxml::xml_document<> itsXML; //!< The XML document
|
rapidxml::xml_document<> itsXML; //!< The XML document
|
||||||
std::stack<NodeInfo> itsNodes; //!< A stack of nodes read from the document
|
std::stack<NodeInfo> itsNodes; //!< A stack of nodes read from the document
|
||||||
};
|
};
|
||||||
|
|||||||
66
sandbox.cpp
66
sandbox.cpp
@@ -279,12 +279,6 @@ int main()
|
|||||||
{
|
{
|
||||||
std::cout << std::boolalpha << std::endl;
|
std::cout << std::boolalpha << std::endl;
|
||||||
|
|
||||||
std::stringstream os;
|
|
||||||
cereal::BinaryOutputArchive archive(os);
|
|
||||||
|
|
||||||
Derived d;
|
|
||||||
archive( d );
|
|
||||||
|
|
||||||
Everything e_out;
|
Everything e_out;
|
||||||
e_out.x = 99;
|
e_out.x = 99;
|
||||||
e_out.y = 100;
|
e_out.y = 100;
|
||||||
@@ -294,36 +288,38 @@ int main()
|
|||||||
e_out.t4 = {4};
|
e_out.t4 = {4};
|
||||||
e_out.s = "Hello, World!";
|
e_out.s = "Hello, World!";
|
||||||
|
|
||||||
Test2 t2 = {22};
|
//Test2 t2 = {22};
|
||||||
|
|
||||||
|
//{
|
||||||
|
// std::ofstream os("out.txt");
|
||||||
|
// cereal::BinaryOutputArchive archive(os);
|
||||||
|
// archive(CEREAL_NVP(e_out));
|
||||||
|
// archive(t2);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//Everything e_in;
|
||||||
|
|
||||||
|
//{
|
||||||
|
// std::ifstream is("out.txt");
|
||||||
|
// cereal::BinaryInputArchive archive(is);
|
||||||
|
// archive(CEREAL_NVP(e_in));
|
||||||
|
// archive(t2);
|
||||||
|
// std::remove("out.txt");
|
||||||
|
//}
|
||||||
|
|
||||||
|
//assert(e_in == e_out);
|
||||||
|
|
||||||
|
//{
|
||||||
|
// cereal::BinaryOutputArchive archive(std::cout);
|
||||||
|
// int xxx[] = {-1, 95, 3};
|
||||||
|
// archive( xxx );
|
||||||
|
|
||||||
|
// cereal::XMLOutputArchive archive2(std::cout);
|
||||||
|
// archive2( xxx );
|
||||||
|
//}
|
||||||
|
|
||||||
|
for( int i = 0; i < 10; ++i )
|
||||||
{
|
{
|
||||||
std::ofstream os("out.txt");
|
|
||||||
cereal::BinaryOutputArchive archive(os);
|
|
||||||
archive(CEREAL_NVP(e_out));
|
|
||||||
archive(t2);
|
|
||||||
}
|
|
||||||
|
|
||||||
Everything e_in;
|
|
||||||
|
|
||||||
{
|
|
||||||
std::ifstream is("out.txt");
|
|
||||||
cereal::BinaryInputArchive archive(is);
|
|
||||||
archive(CEREAL_NVP(e_in));
|
|
||||||
archive(t2);
|
|
||||||
std::remove("out.txt");
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(e_in == e_out);
|
|
||||||
|
|
||||||
{
|
|
||||||
cereal::BinaryOutputArchive archive(std::cout);
|
|
||||||
int xxx[] = {-1, 95, 3};
|
|
||||||
archive( xxx );
|
|
||||||
|
|
||||||
cereal::XMLOutputArchive archive2(std::cout);
|
|
||||||
archive2( xxx );
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
std::ofstream os("out.xml");
|
std::ofstream os("out.xml");
|
||||||
cereal::XMLOutputArchive oar( os );
|
cereal::XMLOutputArchive oar( os );
|
||||||
@@ -378,7 +374,6 @@ int main()
|
|||||||
oar( d3 );
|
oar( d3 );
|
||||||
}
|
}
|
||||||
|
|
||||||
if(false)
|
|
||||||
{
|
{
|
||||||
std::ifstream is("out.xml");
|
std::ifstream is("out.xml");
|
||||||
cereal::XMLInputArchive iar( is );
|
cereal::XMLInputArchive iar( is );
|
||||||
@@ -464,6 +459,7 @@ int main()
|
|||||||
iar( d3 );
|
iar( d3 );
|
||||||
assert( ((Derived*)d3.get())->x == 6 && ((Derived*)d3.get())->y == 5 );
|
assert( ((Derived*)d3.get())->x == 6 && ((Derived*)d3.get())->y == 5 );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -214,6 +214,8 @@ void test_pod()
|
|||||||
float const o_float = random_value<float>(gen);
|
float const o_float = random_value<float>(gen);
|
||||||
double const o_double = random_value<double>(gen);
|
double const o_double = random_value<double>(gen);
|
||||||
|
|
||||||
|
std::cerr << i << std::endl;
|
||||||
|
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
{
|
{
|
||||||
OArchive oar(os);
|
OArchive oar(os);
|
||||||
|
|||||||
Reference in New Issue
Block a user