Search code examples
c++rapidjson

C++ rapidjson Error: free(): invalid next size (normal)


I am reading in data on JavaScript an pass the Jsonstring like that:{"data_size":500, "array":[0,0,0,0,..,0,0]} to the webserver. The numbers in the array could be anything between 0 to 4294967295.

On the Mongoose webserver I am using the lib rapidjson to work with the Jsonstring. Therefore, I create a Document d and reads values from the "jsonstring" into an uint32_t Array using this:

   #include "rapidjson/document.h"

   int i_data_size=0;
   Document d;

   conn->content[conn->content_len]=0;  //to zero terminate

   if (d.Parse(conn->content).HasParseError())
    {
         //Error
    }
    else
    {


     Value& s = d["data_size"];
     i_data_size=s.GetInt();


       uint32_t *Data=NULL;
       Data=new uint32_t[i_data_size];
       Value& a = d["array"];

       for(SizeType i=0;i<a.Size();i++)
       {
         Data[i]=a[i].GetUint();
       }
    }

conn->content is containing the json char*.

When I am sending: {"data_size":500, "array":[0,0,0,0,..,0,0]} everything works find. But sometimes, not everytime, when the a number becomes greater, like this: {"data_size":500, "array":[123,222,0,0,..,0,0]}

I get the Error:

free(): invalid next size (normal)


Solution

  • Solved the problem!

    Writing a zero out of the boundry was causing the error:

    conn->content[conn->content_len]=0;  //to zero terminate
    

    Solved the problem by using a string instead:

      string json="";
        json = string(conn->content);
        json=json.substr(0,conn->content_len);
    
      if (d.Parse(json.c_str()).HasParseError())
      { ...