From a2f0374052855d132c6f47afa0d0ccbf6fa601c6 Mon Sep 17 00:00:00 2001 From: ARATA Mizuki Date: Tue, 23 Feb 2016 16:05:22 +0900 Subject: [PATCH] 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 --- src/common/stringops.cpp | 2 +- tests/strings/unicode.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) 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()