与boost :: property_tree XML解析器一起使用时,boost :: coroutine库崩溃

时间:2016-06-03 20:58:52

标签: c++ boost boost-asio boost-propertytree boost-coroutine

我使用Simple-Web-Server库创建简单的网络服务,将 XML 翻译为 JSON ,反之亦然。反过来,它使用了几个 boost 库以及其中的 boost :: coroutine 。对于 XML< - > JSON 转换,我使用 boost :: property_tree 库进行中间表示。这是代码:

#include <iostream>
#include <sstream>

#include <server_http.hpp>

#define BOOST_SPIRIT_THREADSAFE
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>

using namespace std;
using namespace boost::property_tree;

using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;

int main()
{
  HttpServer server(8080, 1);

  server.resource["^/json_to_xml$"]["POST"] = [](auto& response, auto request) {
    try
    {
      ptree pt;
      read_json(request->content, pt);
      ostringstream json, xml;
      write_json(json, pt);
      clog << "JSON request content:" << endl << json.str() << endl;
      write_xml(xml, pt, xml_writer_make_settings<ptree::key_type>(' ', 1u));
      clog << "XML response content:" << endl << xml.str() << endl;
      response << "HTTP/1.1 200 OK\r\nContent-Length: " << xml.str().length() << "\r\n\r\n" << xml.str();
    }
    catch(const exception& e)
    {
      cerr << "Error:" << endl << e.what() << endl;
      response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what();
    }
  };

  server.resource["^/xml_to_json$"]["POST"] = [](auto& response, auto request) {
    try
    {
      ptree pt;
      read_xml(request->content, pt, xml_parser::trim_whitespace | xml_parser::no_comments);
      ostringstream xml, json;
      write_xml(xml, pt, xml_writer_make_settings<ptree::key_type>(' ', 1u));
      clog << "XML request content:" << endl << xml.str() << endl;
      write_json(json, pt);
      clog << "JSON response content: " << endl << json.str() << endl;
      response << "HTTP/1.1 200 OK\r\nContent-Length: " << json.str().length() << "\r\n\r\n" << json.str();
    }
    catch(const exception& e)
    {
      cerr << "Error:" << endl << e.what() << endl;
      response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what();
    }
  };

  server.start();

  return 0;
}

JSON XML 转换工作正常,但相反导致程序崩溃。当我在服务器的回调中没有使用 boost :: property_tree XML 解析器时,程序运行正常。 Here是执行的结果。 Here GDB 回溯。最后here Valgrind 输出。

boost 库的已使用版本 1.58.0 ,但使用最新版本 1.61.0 时会看到相同的结果。使用 Simple-Web-Server 的版本 1.4.2

1 个答案:

答案 0 :(得分:2)

read_xml()可能会溢出你的coroutine的堆栈;请参阅here,了解类似的崩溃,追溯到rapidxml解析器中的64K堆栈变量。

EDIT。总结链接...问题的关键是,埋在read_xml中的rapidxml解析器在堆栈上分配64K,这会溢出Linux上的8K默认协程栈。您可以尝试两件事......要么强制堆栈变量更小,例如,

#define BOOST_PROPERTY_TREE_RAPIDXML_STATIC_POOL_SIZE 512
#include <boost/property_tree/xml_parser.hpp>
生成协程时

或分配更大的堆栈(如果可以通过Simple-Web-Server访问它)