diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 6030fe0..5a4d156 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -303,7 +303,8 @@ protected: } } else - Transcoder::Transcode(is, *os_); + if (!Transcoder::Transcode(is, *os_)) + return false; } os_->Put('\"'); return true; diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index 54ade34..2a0f484 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -306,3 +306,32 @@ TEST(Writer, RootValueIsComplete) { T(writer.String("")); #undef T } + +TEST(Writer, InvalidEncoding) { + // Fail in decoding invalid UTF-8 sequence http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt + { + GenericStringBuffer > buffer; + Writer >, UTF8<>, UTF16<> > writer(buffer); + writer.StartArray(); + EXPECT_FALSE(writer.String("\xfe")); + EXPECT_FALSE(writer.String("\xff")); + EXPECT_FALSE(writer.String("\xfe\xfe\xff\xff")); + writer.EndArray(); + } + + // Fail in encoding + { + StringBuffer buffer; + Writer > writer(buffer); + static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF + EXPECT_FALSE(writer.String(s)); + } + + // Fail in unicode escaping in ASCII output + { + StringBuffer buffer; + Writer, ASCII<> > writer(buffer); + static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF + EXPECT_FALSE(writer.String(s)); + } +} \ No newline at end of file