Fix check for string termination in IsValidUtf8String()

The current code incorrectly returned true if the string contained
an invalid UTF-8 sequence after an embedded NUL.

Check the entire string if the length was explicitly given instead of stopping
at the first NUL.

Closes https://github.com/wxWidgets/wxWidgets/pull/236
This commit is contained in:
ARATA Mizuki
2016-02-23 16:05:22 +09:00
committed by Vadim Zeitlin
parent e14b589e8e
commit a2f0374052
2 changed files with 9 additions and 1 deletions

View File

@@ -94,7 +94,7 @@ bool wxStringOperationsUtf8::IsValidUtf8String(const char *str, size_t len)
const unsigned char *c = (const unsigned char*)str; const unsigned char *c = (const unsigned char*)str;
const unsigned char * const end = (len == wxStringImpl::npos) ? NULL : c + len; const unsigned char * const end = (len == wxStringImpl::npos) ? NULL : c + len;
for ( ; c != end && *c; ++c ) for ( ; end != NULL ? c != end : *c; ++c )
{ {
unsigned char b = *c; unsigned char b = *c;

View File

@@ -364,6 +364,14 @@ void UnicodeTestCase::ConversionUTF8()
CPPUNIT_ASSERT_EQUAL( 0, c.ToWChar(NULL, 0, u25a6, 0) ); CPPUNIT_ASSERT_EQUAL( 0, c.ToWChar(NULL, 0, u25a6, 0) );
CPPUNIT_ASSERT_EQUAL( 1, c.ToWChar(NULL, 0, u25a6, 3) ); CPPUNIT_ASSERT_EQUAL( 1, c.ToWChar(NULL, 0, u25a6, 3) );
CPPUNIT_ASSERT_EQUAL( 2, c.ToWChar(NULL, 0, u25a6, 4) ); CPPUNIT_ASSERT_EQUAL( 2, c.ToWChar(NULL, 0, u25a6, 4) );
// Verify that converting a string with embedded NULs works.
CPPUNIT_ASSERT_EQUAL( 5, wxString::FromUTF8("abc\0\x32", 5).length() );
// Verify that converting a string containing invalid UTF-8 does not work,
// even if it happens after an embedded NUL.
CPPUNIT_ASSERT( wxString::FromUTF8("abc\xff").empty() );
CPPUNIT_ASSERT( wxString::FromUTF8("abc\0\xff", 5).empty() );
} }
void UnicodeTestCase::ConversionUTF16() void UnicodeTestCase::ConversionUTF16()