Make FLAC::File tolerant to zero-sized padding blocks.
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Wed, 29 Jul 2015 11:52:56 +0000 (20:52 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Wed, 29 Jul 2015 11:52:56 +0000 (20:52 +0900)
taglib/flac/flacfile.cpp
tests/data/zero-sized-padding.flac [new file with mode: 0644]
tests/test_flac.cpp

index 823170aae602d0e40f0548a97edeb651ddfb8aa4..6df8f6b98ae295d2e975104bfbd8d198eb50f541 100644 (file)
@@ -421,9 +421,15 @@ void FLAC::File::scan()
     isLastBlock = (header[0] & 0x80) != 0;
     length = header.toUInt(1U, 3U);
 
-    ByteVector data = readBlock(length);
-    if(data.size() != length || length == 0) {
-      debug("FLAC::File::scan() -- FLAC stream corrupted");
+    if(length == 0 && blockType != MetadataBlock::Padding) {
+      debug("FLAC::File::scan() -- Zero-sized metadaba block found");
+      setValid(false);
+      return;
+    }
+
+    const ByteVector data = readBlock(length);
+    if(data.size() != length) {
+      debug("FLAC::File::scan() -- Failed to read a metadata block");
       setValid(false);
       return;
     }
@@ -446,7 +452,7 @@ void FLAC::File::scan()
         block = picture;
       }
       else {
-        debug("FLAC::File::scan() -- invalid picture found, discarting");
+        debug("FLAC::File::scan() -- invalid picture found, discarding");
         delete picture;
       }
     }
diff --git a/tests/data/zero-sized-padding.flac b/tests/data/zero-sized-padding.flac
new file mode 100644 (file)
index 0000000..86ab8bf
Binary files /dev/null and b/tests/data/zero-sized-padding.flac differ
index f99a679a835c352ffe054b1b45f8d86918158eff..164020a611fd8248c2027206373b4340f3d8288b 100644 (file)
@@ -25,6 +25,7 @@ class TestFLAC : public CppUnit::TestFixture
   CPPUNIT_TEST(testSaveMultipleValues);
   CPPUNIT_TEST(testDict);
   CPPUNIT_TEST(testInvalid);
+  CPPUNIT_TEST(testZeroSizedPadding);
   CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -255,6 +256,14 @@ public:
     CPPUNIT_ASSERT_EQUAL(TagLib::uint(0), f.properties().size());
   }
 
+  void testZeroSizedPadding()
+  {
+    ScopedFileCopy copy("zero-sized-padding", ".flac");
+
+    FLAC::File f(copy.fileName().c_str());
+    CPPUNIT_ASSERT(f.isValid());
+  }
+
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(TestFLAC);