From 90061941390595dbdb71f5f1eb9ccfcdb28d168d Mon Sep 17 00:00:00 2001 From: Gaurav Date: Wed, 15 Feb 2017 17:47:11 +0530 Subject: [PATCH 1/2] Fix uninitialized value detected by valgrind Fix issue reported in https://github.com/open-source-parsers/jsoncpp/issues/578 For std::string variable, length() is more readable than size(). --- src/lib_json/json_reader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 8850d2e..5b6c84a 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -103,7 +103,7 @@ Reader::Reader(const Features& features) bool Reader::parse(const std::string& document, Value& root, bool collectComments) { - JSONCPP_STRING documentCopy(document.data(), document.data() + document.capacity()); + JSONCPP_STRING documentCopy(document.data(), document.data() + document.length()); std::swap(documentCopy, document_); const char* begin = document_.c_str(); const char* end = begin + document_.length(); From 004270db375790e297c1f37ebeca3af1ebf0ca5b Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Sun, 27 Aug 2017 13:47:32 -0500 Subject: [PATCH 2/2] Avoid memory error But simply use `.assign()` instead of the extra copy. (See comment from @BillyDonhue at #580.) fixes #578 closes #580 --- src/lib_json/json_reader.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 5b6c84a..9e346ae 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -103,8 +103,7 @@ Reader::Reader(const Features& features) bool Reader::parse(const std::string& document, Value& root, bool collectComments) { - JSONCPP_STRING documentCopy(document.data(), document.data() + document.length()); - std::swap(documentCopy, document_); + document_.assign(document.begin(), document.end()); const char* begin = document_.c_str(); const char* end = begin + document_.length(); return parse(begin, end, root, collectComments);