From 4d126c49e97da8754ccf82c2ac10476c5fb94592 Mon Sep 17 00:00:00 2001
From: Tsuda Kageyu <tsuda.kageyu@gmail.com>
Date: Sun, 14 Jul 2013 02:28:57 +0900
Subject: [PATCH] Fixed a crash of APE::Item::toString() when the data type is
 binary

---
 examples/framelist.cpp |  5 ++++-
 taglib/ape/apeitem.cpp | 36 +++++++++++++++++++++++-------------
 taglib/ape/apeitem.h   | 10 ++++++----
 3 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/examples/framelist.cpp b/examples/framelist.cpp
index dbe80feb..679aa393 100644
--- a/examples/framelist.cpp
+++ b/examples/framelist.cpp
@@ -95,7 +95,10 @@ int main(int argc, char *argv[])
       for(APE::ItemListMap::ConstIterator it = ape->itemListMap().begin();
           it != ape->itemListMap().end(); ++it)
       {
-        cout << (*it).first << " - \"" << (*it).second.toString() << "\"" << endl;
+        if((*it).second.type() != APE::Item::Binary)
+          cout << (*it).first << " - \"" << (*it).second.toString() << "\"" << endl;
+        else
+          cout << (*it).first << " - Binary data (" << (*it).second.binaryData().size() << " bytes)" << endl;
       }
     }
     else
diff --git a/taglib/ape/apeitem.cpp b/taglib/ape/apeitem.cpp
index b1670a65..3490173a 100644
--- a/taglib/ape/apeitem.cpp
+++ b/taglib/ape/apeitem.cpp
@@ -125,6 +125,7 @@ void APE::Item::setBinaryData(const ByteVector &value)
 {
   d->type = Binary;
   d->value = value;
+  d->text.clear();
 }
 
 ByteVector APE::Item::value() const
@@ -137,31 +138,35 @@ ByteVector APE::Item::value() const
 
 void APE::Item::setKey(const String &key)
 {
-    d->key = key;
+  d->key = key;
 }
 
 void APE::Item::setValue(const String &value)
 {
-    d->type = Text;
-    d->text = value;
+  d->type = Text;
+  d->text = value;
+  d->value.clear();
 }
 
 void APE::Item::setValues(const StringList &value)
 {
-    d->type = Text;
-    d->text = value;
+  d->type = Text;
+  d->text = value;
+  d->value.clear();
 }
 
 void APE::Item::appendValue(const String &value)
 {
-    d->type = Text;
-    d->text.append(value);
+  d->type = Text;
+  d->text.append(value);
+  d->value.clear();
 }
 
 void APE::Item::appendValues(const StringList &values)
 {
-    d->type = Text;
-    d->text.append(values);
+  d->type = Text;
+  d->text.append(values);
+  d->value.clear();
 }
 
 int APE::Item::size() const
@@ -200,7 +205,10 @@ StringList APE::Item::values() const
 
 String APE::Item::toString() const
 {
-  return isEmpty() ? String::null : d->text.front();
+  if(d->type == Text && !isEmpty())
+    return d->text.front();
+  else
+    return String::null;
 }
 
 bool APE::Item::isEmpty() const
@@ -234,13 +242,15 @@ void APE::Item::parse(const ByteVector &data)
 
   d->key = String(data.mid(8), String::UTF8);
 
-  d->value = data.mid(8 + d->key.size() + 1, valueLength);
+  const ByteVector value = data.mid(8 + d->key.size() + 1, valueLength);
 
   setReadOnly(flags & 1);
   setType(ItemTypes((flags >> 1) & 3));
 
-  if(Text == d->type)
-    d->text = StringList(ByteVectorList::split(d->value, '\0'), String::UTF8);
+  if(Text == d->type) 
+    d->text = StringList(ByteVectorList::split(value, '\0'), String::UTF8);
+  else
+    d->value = value;
 }
 
 ByteVector APE::Item::render() const
diff --git a/taglib/ape/apeitem.h b/taglib/ape/apeitem.h
index f7fd05e3..0588d185 100644
--- a/taglib/ape/apeitem.h
+++ b/taglib/ape/apeitem.h
@@ -97,7 +97,7 @@ namespace TagLib {
 
       /*!
        * Returns the binary value.
-       * If the item type is not \a Binary, the returned contents are undefined
+       * If the item type is not \a Binary, always returns an empty ByteVector.
        */
       ByteVector binaryData() const;
 
@@ -152,8 +152,9 @@ namespace TagLib {
       int size() const;
 
       /*!
-       * Returns the value as a single string. In case of multiple strings,
-       * the first is returned.
+       * Returns the value as a single string.  In case of multiple strings,
+       * the first is returned.  If the data type is not \a Text, always returns 
+       * an empty String.
        */
       String toString() const;
 
@@ -163,7 +164,8 @@ namespace TagLib {
 #endif
 
       /*!
-       * Returns the list of text values.
+       * Returns the list of text values.  If the data type is not \a Text, always 
+       * returns an empty StringList.
        */
       StringList values() const;
 
-- 
2.40.0