fix off by one bug in the buffer size (fixes #10039)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_8_BRANCH@56246 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-10-12 14:51:45 +00:00
parent cebbcdcf5e
commit 27895e2f37
3 changed files with 55 additions and 3 deletions

View File

@@ -81,6 +81,7 @@ private:
CPPUNIT_TEST( LibcTests );
CPPUNIT_TEST( IconvTests );
CPPUNIT_TEST( FontmapTests );
CPPUNIT_TEST( BufSize );
#ifdef HAVE_WCHAR_H
CPPUNIT_TEST( UTF8_41 );
CPPUNIT_TEST( UTF8_7f );
@@ -114,6 +115,7 @@ private:
void CP1252Tests();
void LibcTests();
void FontmapTests();
void BufSize();
void IconvTests();
// verifies that the specified multibyte sequence decodes to the specified wchar_t sequence
@@ -799,6 +801,48 @@ void MBConvTestCase::FontmapTests()
#endif
}
void MBConvTestCase::BufSize()
{
wxCSConv conv1251(_T("CP1251"));
CPPUNIT_ASSERT( conv1251.IsOk() );
const char *cp1251text =
"\313\301\326\305\324\323\321 \325\304\301\336\316\331\315";
const size_t cp1251textLen = strlen(cp1251text);
const size_t lenW = conv1251.MB2WC(NULL, cp1251text, cp1251textLen);
CPPUNIT_ASSERT( lenW != wxCONV_FAILED );
wxWCharBuffer wbuf(lenW);
// lenW-1 is not enough
CPPUNIT_ASSERT_EQUAL(
wxCONV_FAILED, conv1251.MB2WC(wbuf.data(), cp1251text, lenW - 1) );
// lenW is just fine
CPPUNIT_ASSERT(
conv1251.MB2WC(wbuf.data(), cp1251text, lenW) != wxCONV_FAILED );
// of course, greater values work too
CPPUNIT_ASSERT(
conv1251.MB2WC(wbuf.data(), cp1251text, lenW + 1) != wxCONV_FAILED );
// test in the other direction too, using an encoding with multibyte NUL
wxCSConv convUTF16(_T("UTF-16"));
CPPUNIT_ASSERT( convUTF16.IsOk() );
const wchar_t *utf16text = L"Hello";
const size_t utf16textLen = wcslen(utf16text);
const size_t lenMB = convUTF16.WC2MB(NULL, utf16text, utf16textLen);
CPPUNIT_ASSERT( lenMB != wxCONV_FAILED );
wxCharBuffer buf(lenMB + 1); // it only adds 1 for NUL on its own, we need 2
CPPUNIT_ASSERT_EQUAL(
wxCONV_FAILED, convUTF16.WC2MB(buf.data(), utf16text, lenMB - 1) );
CPPUNIT_ASSERT(
convUTF16.WC2MB(buf.data(), utf16text, lenMB) != wxCONV_FAILED );
CPPUNIT_ASSERT(
convUTF16.WC2MB(buf.data(), utf16text, lenMB + 1) != wxCONV_FAILED );
}
WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_iconv( const wxChar* name );