From 9da20a8a52b6c7ab0c0f0004be354fbc1b225d30 Mon Sep 17 00:00:00 2001
From: Tsuda Kageyu <tsuda.kageyu@gmail.com>
Date: Mon, 25 May 2015 00:40:13 +0900
Subject: [PATCH] Skip duplicate chunks when reading AIFF/WAV files.

Similar to #492.
There is no good reason to use the last chunk rather than the first one.
---
 taglib/riff/aiff/aifffile.cpp | 10 ++++++++--
 taglib/riff/wav/wavfile.cpp   | 20 ++++++++++++++++----
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/taglib/riff/aiff/aifffile.cpp b/taglib/riff/aiff/aifffile.cpp
index 595b2950..c7cadb67 100644
--- a/taglib/riff/aiff/aifffile.cpp
+++ b/taglib/riff/aiff/aifffile.cpp
@@ -150,8 +150,14 @@ void RIFF::AIFF::File::read(bool readProperties, Properties::ReadStyle propertie
         debug("RIFF::AIFF::File::read() - Duplicate ID3v2 tag found.");
       }
     }
-    else if(name == "COMM" && readProperties)
-      formatData = chunkData(i);
+    else if(name == "COMM" && readProperties) {
+      if(formatData.isEmpty()) {
+        formatData = chunkData(i);
+      }
+      else {
+        debug("RIFF::AIFF::File::read() - Duplicate 'COMM' chunk found.");
+      }
+    }
   }
 
   if(!d->tag)
diff --git a/taglib/riff/wav/wavfile.cpp b/taglib/riff/wav/wavfile.cpp
index 4b379fa4..aa367b5d 100644
--- a/taglib/riff/wav/wavfile.cpp
+++ b/taglib/riff/wav/wavfile.cpp
@@ -217,10 +217,22 @@ void RIFF::WAV::File::read(bool readProperties, Properties::ReadStyle properties
         }
       }
     }
-    else if(name == "fmt " && readProperties)
-      formatData = chunkData(i);
-    else if(name == "data" && readProperties)
-      streamLength = chunkDataSize(i);
+    else if(name == "fmt " && readProperties) {
+      if(formatData.isEmpty()) {
+        formatData = chunkData(i);
+      }
+      else {
+        debug("RIFF::WAV::File::read() - Duplicate 'fmt ' chunk found.");
+      }
+    }
+    else if(name == "data" && readProperties) {
+      if(streamLength == 0) {
+        streamLength = chunkDataSize(i);
+      }
+      else {
+        debug("RIFF::WAV::File::read() - Duplicate 'data' chunk found.");
+      }
+    }
   }
 
   if(!d->tag[ID3v2Index])
-- 
2.40.0