From 038b52ae01060089ee6d260e83df258725d8c1d4 Mon Sep 17 00:00:00 2001
From: Tsuda Kageyu <tsuda.kageyu@gmail.com>
Date: Mon, 30 Jan 2017 11:35:39 +0900
Subject: [PATCH] Check an invalid UTF-8 sequence consists of single char.

Single char can be an invalid UTF sequence. For example, { 0x80 } is invalid.
---
 taglib/toolkit/tstring.cpp | 12 ++++++------
 tests/test_string.cpp      |  9 +++++++--
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp
index 863bbf59..f02195c4 100644
--- a/taglib/toolkit/tstring.cpp
+++ b/taglib/toolkit/tstring.cpp
@@ -231,10 +231,6 @@ public:
   StringPrivate() :
     RefCounter() {}
 
-  StringPrivate(unsigned int n, wchar_t c) :
-    RefCounter(),
-    data(static_cast<size_t>(n), c) {}
-
   /*!
    * Stores string in UTF-16. The byte order depends on the CPU endian.
    */
@@ -334,9 +330,13 @@ String::String(wchar_t c, Type t) :
 }
 
 String::String(char c, Type t) :
-  d(new StringPrivate(1, static_cast<unsigned char>(c)))
+  d(new StringPrivate())
 {
-  if(t != Latin1 && t != UTF8) {
+  if(t == Latin1)
+    copyFromLatin1(d->data, &c, 1);
+  else if(t == String::UTF8)
+    copyFromUTF8(d->data, &c, 1);
+  else {
     debug("String::String() -- char should not contain UTF16.");
   }
 }
diff --git a/tests/test_string.cpp b/tests/test_string.cpp
index d3661c24..e460bfa1 100644
--- a/tests/test_string.cpp
+++ b/tests/test_string.cpp
@@ -50,7 +50,7 @@ class TestString : public CppUnit::TestFixture
   CPPUNIT_TEST(testEncodeNonLatin1);
   CPPUNIT_TEST(testEncodeEmpty);
   CPPUNIT_TEST(testIterator);
-  CPPUNIT_TEST(testRedundantUTF8);
+  CPPUNIT_TEST(testInvalidUTF8);
   CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -331,12 +331,17 @@ public:
     CPPUNIT_ASSERT_EQUAL(L'I', *it2);
   }
 
-  void testRedundantUTF8()
+  void testInvalidUTF8()
   {
     CPPUNIT_ASSERT_EQUAL(String("/"), String(ByteVector("\x2F"), String::UTF8));
     CPPUNIT_ASSERT(String(ByteVector("\xC0\xAF"), String::UTF8).isEmpty());
     CPPUNIT_ASSERT(String(ByteVector("\xE0\x80\xAF"), String::UTF8).isEmpty());
     CPPUNIT_ASSERT(String(ByteVector("\xF0\x80\x80\xAF"), String::UTF8).isEmpty());
+
+    CPPUNIT_ASSERT(String(ByteVector("\xF8\x80\x80\x80\x80"), String::UTF8).isEmpty());
+    CPPUNIT_ASSERT(String(ByteVector("\xFC\x80\x80\x80\x80\x80"), String::UTF8).isEmpty());
+
+    CPPUNIT_ASSERT(String('\x80', String::UTF8).isEmpty());
   }
 
 };
-- 
2.40.0