From 953cda14f709b4915f8cbcae187841641abb4821 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Tue, 31 Mar 2015 20:55:00 +0200 Subject: [PATCH] tutorial.cpp: fix insitu parsing (closes #273) The insitu parsing example in the tutorial is using a local char buffer inside a nested scope. After leaving the scope, the resulting Document object still references this (now invalid) memory locations. Some compilers reuse this space on the stack for other local variables later in the function, corrupting the original document. Drop the local scope to extend the lifetime of the buffer. --- example/tutorial/tutorial.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/example/tutorial/tutorial.cpp b/example/tutorial/tutorial.cpp index 57f94a5..49704c1 100644 --- a/example/tutorial/tutorial.cpp +++ b/example/tutorial/tutorial.cpp @@ -24,12 +24,10 @@ int main(int, char*[]) { return 1; #else // In-situ parsing, decode strings directly in the source string. Source must be string. - { - char buffer[sizeof(json)]; - memcpy(buffer, json, sizeof(json)); - if (document.ParseInsitu(buffer).HasParseError()) - return 1; - } + char buffer[sizeof(json)]; + memcpy(buffer, json, sizeof(json)); + if (document.ParseInsitu(buffer).HasParseError()) + return 1; #endif printf("\nParsing to document succeeded.\n");