Skip to content

Commit

Permalink
Fix crash with invalid WAV files (taglib#1163)
Browse files Browse the repository at this point in the history
With specially crafted WAV files having the "id3 " chunk as the
only valid chunk, when trying to write the tags, the existing
"id3 " chunk is removed, and then vector::front() is called on
the now empty chunks vector.
Now it is checked if the vector is empty to avoid the crash.
  • Loading branch information
ufleisch committed Nov 3, 2023
1 parent 3869aa1 commit 5b70bf8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions taglib/riff/rifffile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ void RIFF::File::writeChunk(const ByteVector &name, const ByteVector &data,

void RIFF::File::updateGlobalSize()
{
if(d->chunks.empty())
return;

const Chunk first = d->chunks.front();
const Chunk last = d->chunks.back();
d->size = static_cast<unsigned int>(last.offset + last.size + last.padding - first.offset + 12);
Expand Down
Binary file added tests/data/invalid-chunk.wav
Binary file not shown.
18 changes: 18 additions & 0 deletions tests/test_wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class TestWAV : public CppUnit::TestFixture
CPPUNIT_TEST(testStripAndProperties);
CPPUNIT_TEST(testPCMWithFactChunk);
CPPUNIT_TEST(testWaveFormatExtensible);
CPPUNIT_TEST(testInvalidChunk);
CPPUNIT_TEST_SUITE_END();

public:
Expand Down Expand Up @@ -385,6 +386,23 @@ class TestWAV : public CppUnit::TestFixture
CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->format());
}

void testInvalidChunk()
{
ScopedFileCopy copy("invalid-chunk", ".wav");

{
RIFF::WAV::File f(copy.fileName().c_str());
CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->lengthInSeconds());
CPPUNIT_ASSERT(f.hasID3v2Tag());
f.ID3v2Tag()->setTitle("Title");
f.save();
}
{
RIFF::WAV::File f(copy.fileName().c_str());
CPPUNIT_ASSERT(!f.hasID3v2Tag());
}
}

};

CPPUNIT_TEST_SUITE_REGISTRATION(TestWAV);

0 comments on commit 5b70bf8

Please sign in to comment.