open-source-parsers / open-source-parsers/jsoncpp

Linking debug .dll (via vcpkg) to debug build of windows app causes string corruption issues

Open
#1,465 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug build
Dominant language
C++
Stars
8.9k
Forks
2.7k
Avg merge
31m
Merged PRs (30d)
1

Description

Describe the bug
[updated] - i made some progress on uncovering the root cause; updated title and description to reflect my current understanding

When linking the debug .dll and .lib (generated via vcpkg) to debug build of an Visual C++ project, any string assignment from a jsoncpp library function to the main app (also in debug mode) will cause garbage to be written and crash the app.

I believe this is due to a mismatch linking CRT between the library and the app. I can reproduce a similar behavior with other libraries (such as, say, libcurl) by linking the release version of the dll against debug build of the app. Stack Overflow has a nice explanation.

To Reproduce

  • install jsoncpp 1.9.5 via vcpkg on windows .\vcpkg.exe install jsoncpp:x64-windows
  • create simple .exe application that outputs json string, linking to the debug .lib and .dll files. I tried both with StreamWriterBuilder and FastWriter.
#include <json/json.h>
#include <iostream>

int main(int argc, char* argv[])
{
	Json::Value payloadJson;
	payloadJson["foo"] = "bar";
	
	Json::StreamWriterBuilder builder;
	builder["indentation"] = "";
	std::string output = Json::writeString(builder, payloadJson);
	//Json::FastWriter writer;
	//std::string output = writer.write(payloadJson);

	std::cout << output;

	return 0;
}
  • with StreamWriterBuilder, it crashes on assigning indentation string:
Exception thrown at 0x00007FFA8C6F1490 (vcruntime140.dll) in jsoncpptest.exe: 0xC0000005: Access violation reading location 0x000000565D9E6871.

vcruntime140.dll!00007ffa8c6f1490()
[Inline Frame] jsoncpp.dll!Json::duplicateStringValue(const char * value, unsigned __int64) Line 126
	at C:\Users\david\src\vcpkg\buildtrees\jsoncpp\src\1.9.5-13b47286ba.clean\src\lib_json\json_value.cpp(126)
jsoncpp.dll!Json::Value::CZString::CZString(const Json::Value::CZString & other) Line 247
	at C:\Users\david\src\vcpkg\buildtrees\jsoncpp\src\1.9.5-13b47286ba.clean\src\lib_json\json_value.cpp(247)
[Inline Frame] jsoncpp.dll!std::pair<Json::Value::CZString const ,Json::Value>::{ctor}(Json::Value::CZString & _Val2, const Json::Value &) Line 172
	at C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\include\utility(172)
jsoncpp.dll!Json::Value::resolveReference(const char * key, const char * end) Line 1069
	at C:\Users\david\src\vcpkg\buildtrees\jsoncpp\src\1.9.5-13b47286ba.clean\src\lib_json\json_value.cpp(1069)
jsoncpptest.exe!main(int argc, char * * argv) Line 10
	at C:\Users\david\src\jsoncpptest\main.cpp(10)
  • with FastWriter it crashes when write() returns NULL instead of the string.
  • I can confirmed that the JSON value contains the expected information.

Expected behavior
The same code works as expected on OSX.

Desktop

  • OS: Windows 10 Pro 21H2
  • Visual C++ 2022 (Visual Studio 17.4.4)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with jsoncpp 1.9.5 installed through vcpkg and the sample in main.cpp. Compare the debug and release DLL/.lib combinations and the CRT settings used by the Visual C++ application and jsoncpp; the stack trace points to src/lib_json/json_value.cpp. Done means the sample runs without string corruption or a crash when using matching debug builds.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
build-system, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.