Menu

#22 Percent symbol replaced with the word "Success" in cdata

v1.0 (example)
open
nobody
None
5
2014-07-03
2014-05-16
Seth Call
No

My code looks like this:
std::string str = "% mem=2000MB"; // This stays in scope until after the document is written
xml_node<>* text = doc.allocate_node(node_cdata, NULL, str.c_str(), 0, str.length());
...

This text is being written in the cdata tag:
<![CDATA[Successem=2000MB]]>

Discussion

  • Seth Call

    Seth Call - 2014-05-17

    Here's a complete sample program:

    #include <stdio.h>
    #include <string>
    #include "rapidxml/rapidxml.hpp"
    #include "rapidxml/rapidxml_utils.hpp"
    #include "rapidxml/rapidxml_print.hpp"
    
    int main(int argc, char* argv[])
    {
        using namespace rapidxml;
        const char* fileName = "test.xml";
        printf("Writing file: %s\n", fileName);
        xml_document<> doc;
    
        xml_node<>* test = doc.allocate_node(node_element, "test");
        doc.append_node(test);
        std::string str = "% mem=2000MB";
        xml_node<>* text = doc.allocate_node(node_cdata, NULL, str.c_str(), 0, str.length());
        test->append_node(text);
    
        FILE *f = fopen(fileName, "w");
        if (f == NULL) {
            printf("Error writing file: %s\n", fileName);
            return false;
        }
    
        std::string s;
        print(std::back_inserter(s), doc, 0);
        fprintf(f, s.c_str());
        fclose(f);
    
        return 0;
    }
    

    The output is:
    <test>
     <![CDATA[Successem=2000MB]]>
    </test>

     
  • Bernd

    Bernd - 2014-07-02

    Perhaps you should NOT use "fprintf" if you simply want to print the content of a string - a common programming error (just because we use this (f)printf so often) and also a source of exploits :)
    Use fputs instead or "fprintf(f,"%s",s.c_str());".

     
  • Seth Call

    Seth Call - 2014-07-03

    Should have caught that myself. Thanks!

     

Log in to post a comment.