Add a missing error handling for Writer, and add tests for invalid encoding.

This commit is contained in:
miloyip 2015-04-13 14:05:43 +08:00
parent 79433827e8
commit 127ce7175a
2 changed files with 31 additions and 1 deletions

View File

@ -303,7 +303,8 @@ protected:
}
}
else
Transcoder<SourceEncoding, TargetEncoding>::Transcode(is, *os_);
if (!Transcoder<SourceEncoding, TargetEncoding>::Transcode(is, *os_))
return false;
}
os_->Put('\"');
return true;

View File

@ -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<UTF16<> > buffer;
Writer<GenericStringBuffer<UTF16<> >, 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<StringBuffer, UTF32<> > 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<StringBuffer, UTF32<>, ASCII<> > writer(buffer);
static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF
EXPECT_FALSE(writer.String(s));
}
}