Skip to content

Run-Time Check Failure #2 - Stack around the variable 'files' was corrupted. #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
circusjoker06 opened this issue Apr 13, 2016 · 4 comments

Comments

@circusjoker06
Copy link

int main(){
  Json::Reader reader;
  try
  {
    Json::Value root;

    const char* str2 = "{\"uploadid\": \"UP000000\",\"code\":-2105368552146909364,\"msg\": \"\",\"files\":[]}";

    if (reader.parse(str2, root))

    {

      string upload_id = root["uploadid"].asString();

      //int code = root["code"].asInt();

      string code =root["code"].asString();
      string msg = root["msg"].asString();
      Json::Value files =root["files"];
      {
        cout<<"files empty"<<endl;
      }
      cout<<root.size()<<" "<<upload_id<<" "<<code<<" "<<msg<<endl;
    }

  }
  catch(std::exception e)
  {
    // (1) it will catch the error show show
    cerr << e.what() << endl;
  }
  catch(std::out_of_range e)
  {
    // (2) this will also catch the same error if (1) was not there but could
    // get you more details if you wanted since its more specific but i have
    // not digged into it further
    cerr << e.what() << endl;
  }
  catch(...)
  {
    // (3) just for sanity check if first two didn't catch it
    cerr << "something went wrong";
  }
}
@circusjoker06
Copy link
Author

error:Run-Time Check Failure #2 - Stack around the variable 'files' was corrupted.

@cdunn2001
Copy link
Contributor

Works for me, on a recent MacAir with up-to-date o/s and compiler. Try the tip of the master branch.

@sergiy80
Copy link
Contributor

sergiy80 commented Nov 28, 2016

@cdunn2001 ,
I could reproduce this. This happens because of struct packing size of json source files differ from packing size of my source files where json.h is included.
I am using VS 2013.
To reproduce build jsoncpp (I use amalgamated version) as a static lib with packing size 8 (default in VS2013). Use this static lib in another c++ project with packing size 4, 2 or 1, or just change packing size before including json.h. Start - got debug error from VS.

Also note sizeof(Json::StreamWriterBuilder) is changing depending on active packing size. I have 28 bytes - for sizes 1,2,4, and 32 bytes - for 8 and 16.
I thin this is the reason of the error.

To fix: add pragma pack guards in all jsoncpp headers, so that jsoncpp code does not depend on external packing size.
This also can be a reason of issues like: #391, #338

Sample is below:

#pragma pack(push, 2)
#include "json/json.h"
#pragma pack(pop)

void Run()
{
	std::cout << "Json::Value sizeof = " << sizeof(Json::Value) << std::endl;
	std::cout << "Json::StreamWriterBuilder sizeof = " << sizeof(Json::StreamWriterBuilder) << std::endl;

	Json::StreamWriterBuilder builder;
	std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
	Json::Value v;
	v["val1"] = 123;
	writer->write(v, &std::cout);
	std::cout << std::endl;
}

@cdunn2001
Copy link
Contributor

@sergiy80, thanks for the analysis. This seems like a good idea. Would you like to submit the pull-request?

sergiy80 added a commit to sergiy80/jsoncpp that referenced this issue Dec 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants