Skip to content

Commit

Permalink
Keep Document value unchanged on parse error, fixes Tencent#437
Browse files Browse the repository at this point in the history
Keeping the DOM unchanged in case of an error is the intended
behaviour according to the [documentation] [1].

Instead of forcing the value to `kNullType` before starting the
parsing, store the parsed value upon success via regular move.

[1]: https://miloyip.github.io/rapidjson/md_doc_dom.html#ParseError
  • Loading branch information
pah committed Oct 7, 2015
1 parent 221887d commit fa12369
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions include/rapidjson/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -1886,14 +1886,13 @@ class GenericDocument : public GenericValue<Encoding, Allocator> {
*/
template <unsigned parseFlags, typename SourceEncoding, typename InputStream>
GenericDocument& ParseStream(InputStream& is) {
ValueType::SetNull(); // Remove existing root if exist
GenericReader<SourceEncoding, Encoding, StackAllocator> reader(
stack_.HasAllocator() ? &stack_.GetAllocator() : 0);
ClearStackOnExit scope(*this);
parseResult_ = reader.template Parse<parseFlags>(is, *this);
if (parseResult_) {
RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object
this->RawAssign(*stack_.template Pop<ValueType>(1)); // Add this-> to prevent issue 13.
ValueType::operator=(*stack_.template Pop<ValueType>(1));// Move value from stack to document
}
return *this;
}
Expand Down

0 comments on commit fa12369

Please sign in to comment.