Fix MemoryStream::Peek() and add test for fix

MemoryStream::Peek() did not return '\0' if src_ == end_, but Peek() == '\0' is
used in parsing in the GenericReader. Without this change, parsing with
MemoryStream as the InputStream could result in a segmentation fault.
This commit is contained in:
Sean Leather 2014-10-23 00:24:32 +02:00
parent 98f87905d9
commit 0e715872c0
2 changed files with 11 additions and 1 deletions

View File

@ -42,7 +42,7 @@ struct MemoryStream {
MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {}
Ch Peek() const { return *src_; }
Ch Peek() const { return (src_ == end_) ? '\0' : *src_; }
Ch Take() { return (src_ == end_) ? '\0' : *src_++; }
size_t Tell() const { return static_cast<size_t>(src_ - begin_); }

View File

@ -21,6 +21,7 @@
#include "unittest.h"
#include "rapidjson/reader.h"
#include "rapidjson/memorystream.h"
using namespace rapidjson;
@ -674,6 +675,15 @@ TEST(Reader, ParseObject_Error) {
// Must be a comma or '}' after an object member
TEST_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, "{\"a\":1]");
// This tests that MemoryStream is checking the length in Peek().
{
MemoryStream ms("{\"a\"", 1);
BaseReaderHandler<> h;
Reader reader;
EXPECT_FALSE(reader.Parse<kParseStopWhenDoneFlag>(ms, h));
EXPECT_EQ(kParseErrorObjectMissName, reader.GetParseErrorCode());
}
}
#undef TEST_ERROR