Question

I am trying to create a member function for setting up a rapidxml::xml_document<> object from a string, the overloaded ones for std::string, const std::string and const char * work fine.

When I try to load the char * directly, I get a buffer overflow, using a copied string works (which I would like to avoid, because of long strings).

My System: Debian testing with g++ 4.8.2

EDIT: I am aware that the string will be modified by rapidxml (for const char * objects, therefore I create a copy (per std::vector))

example code, which produces the same overflow follows:

#include <iostream>
#include <vector>
#include "rapidxml.hpp"   // RapidXml 1.13


int main() {
    char * str = (char *)"<efa><departures>data</departures></efa>";
    rapidxml::xml_document<> doc;

// I'd like to avoid the copying in the following code block
#   if 0
        std::vector<char> writable;
        if (str) {
            while (* str) {
                writable.push_back(* str);
                str++;
            }
        } else {
            writable.push_back('\0');
        }
#       define str &writable[0]
#   endif

    std::cout << "pre" << std::endl;
    doc.parse<rapidxml::parse_no_data_nodes> (str);
    std::cout << "post" << std::endl;

    std::cout << doc.first_node()->name() << std::endl;
}
Was it helpful?

Solution

str is pointing to a string literal, so modifying it is illegal.

From the docs:

function xml_document::parse

[...] Passed string will be modified by the parser [...]

You could change the declaration to

char str[] = "<efa><departures>data</departures></efa>";

OTHER TIPS

RapidXML modifies input string, so passing the literal causes a problem. However, the docs say Passed string will be modified by the parser, unless rapidxml::parse_non_destructive flag is used. so I guess the best way would be to use this flag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top