From 0e715872c0b8b67d2050e5413016869bf399e6b8 Mon Sep 17 00:00:00 2001 From: Sean Leather Date: Thu, 23 Oct 2014 00:24:32 +0200 Subject: [PATCH] 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. --- include/rapidjson/memorystream.h | 2 +- test/unittest/readertest.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/memorystream.h b/include/rapidjson/memorystream.h index 8701c33..6ed226e 100644 --- a/include/rapidjson/memorystream.h +++ b/include/rapidjson/memorystream.h @@ -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(src_ - begin_); } diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index b42d832..9f7d853 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -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(ms, h)); + EXPECT_EQ(kParseErrorObjectMissName, reader.GetParseErrorCode()); + } } #undef TEST_ERROR