diff --git a/src/common/stringops.cpp b/src/common/stringops.cpp index 1592706872..36ff4045a4 100644 --- a/src/common/stringops.cpp +++ b/src/common/stringops.cpp @@ -94,7 +94,7 @@ bool wxStringOperationsUtf8::IsValidUtf8String(const char *str, size_t len) const unsigned char *c = (const unsigned char*)str; 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; diff --git a/tests/strings/unicode.cpp b/tests/strings/unicode.cpp index 20751f23fe..19587dd99f 100644 --- a/tests/strings/unicode.cpp +++ b/tests/strings/unicode.cpp @@ -364,6 +364,14 @@ void UnicodeTestCase::ConversionUTF8() CPPUNIT_ASSERT_EQUAL( 0, c.ToWChar(NULL, 0, u25a6, 0) ); CPPUNIT_ASSERT_EQUAL( 1, c.ToWChar(NULL, 0, u25a6, 3) ); 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()