Fix out of bounds read with kParseValidateEncodingFlag
diff --git a/include/rapidjson/encodings.h b/include/rapidjson/encodings.h
index 50ad18b..c453c0d 100644
--- a/include/rapidjson/encodings.h
+++ b/include/rapidjson/encodings.h
@@ -177,10 +177,10 @@
 
     template <typename InputStream, typename OutputStream>
     static bool Validate(InputStream& is, OutputStream& os) {
-#define RAPIDJSON_COPY() os.Put(c = is.Take())
+#define RAPIDJSON_COPY() if (c != '\0') os.Put(c = is.Take())
 #define RAPIDJSON_TRANS(mask) result &= ((GetRange(static_cast<unsigned char>(c)) & mask) != 0)
 #define RAPIDJSON_TAIL() RAPIDJSON_COPY(); RAPIDJSON_TRANS(0x70)
-        Ch c;
+        Ch c = static_cast<Ch>(-1);
         RAPIDJSON_COPY();
         if (!(c & 0x80))
             return true;
diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp
index 8ff67d1..dec3da6 100644
--- a/test/unittest/readertest.cpp
+++ b/test/unittest/readertest.cpp
@@ -987,6 +987,24 @@
         }
     }
 
+    // 3.6 Lonely start characters near the end of the input
+    {
+        char e[] = { '\"', 0, '\"', '\0' };
+        for (unsigned c = 0xC0u; c <= 0xFFu; c++) {
+            e[1] = static_cast<char>(c);
+            unsigned streamPos;
+            if (c <= 0xC1u)
+                streamPos = 2; // 0xC0 - 0xC1
+            else if (c <= 0xDFu)
+                streamPos = 3; // 0xC2 - 0xDF
+            else if (c <= 0xF4u)
+                streamPos = 4; // 0xE0 - 0xF4
+            else
+                streamPos = 2; // 0xF5 - 0xFF
+            TEST_STRING_ERROR(kParseErrorStringInvalidEncoding, e, 1u, streamPos);
+        }
+    }
+
     // 4  Overlong sequences
 
     // 4.1  Examples of an overlong ASCII character