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.
This commit is contained in:
Philipp A. Hartmann 2015-03-31 20:55:00 +02:00
parent b484407999
commit 953cda14f7

View File

@ -24,12 +24,10 @@ int main(int, char*[]) {
return 1; return 1;
#else #else
// In-situ parsing, decode strings directly in the source string. Source must be string. // In-situ parsing, decode strings directly in the source string. Source must be string.
{ char buffer[sizeof(json)];
char buffer[sizeof(json)]; memcpy(buffer, json, sizeof(json));
memcpy(buffer, json, sizeof(json)); if (document.ParseInsitu(buffer).HasParseError())
if (document.ParseInsitu(buffer).HasParseError()) return 1;
return 1;
}
#endif #endif
printf("\nParsing to document succeeded.\n"); printf("\nParsing to document succeeded.\n");