Skip to content

Commit d6a2134

Browse files
Clamp oversized RIFF chunk to available bytes instead of rejecting it (#1329)
Some encoders write a valid data chunk but with a slightly too-large declared chunkSize, or place the data chunk beyond the declared RIFF boundary. The previous behaviour called break, abandoning all remaining chunks and making the file appear empty to taglib. Lenient parsers (ffmpeg, QuickTime) handle this case by clamping the chunk size to the bytes that actually remain in the file. Adopt the same strategy: when chunkSize would exceed the file length, clamp it and continue parsing rather than stopping early.
1 parent abadbb6 commit d6a2134

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

taglib/riff/rifffile.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,20 @@ void RIFF::File::read()
298298

299299
seek(offset);
300300
const ByteVector chnkName = readBlock(4);
301-
const unsigned int chunkSize = readBlock(4).toUInt(bigEndian);
301+
unsigned int chunkSize = readBlock(4).toUInt(bigEndian);
302302

303303
if(!isValidChunkName(chnkName)) {
304304
debug("RIFF::File::read() -- Chunk '" + chnkName + "' has invalid ID");
305305
break;
306306
}
307307

308308
if(static_cast<long long>(offset) + 8 + chunkSize > length()) {
309-
debug("RIFF::File::read() -- Chunk '" + chnkName + "' has invalid size (larger than the file size)");
310-
break;
309+
// Clamp to available bytes rather than rejecting the chunk outright.
310+
// Some encoders write a correct data chunk but with a slightly too-large
311+
// declared size, or place the data chunk outside the declared RIFF boundary.
312+
// Lenient parsers (ffmpeg, QuickTime) handle this by clamping; we do the same.
313+
debug("RIFF::File::read() -- Chunk '" + chnkName + "' is truncated; clamping size to available bytes.");
314+
chunkSize = static_cast<unsigned int>(length() - offset - 8);
311315
}
312316

313317
Chunk chunk;

tests/test_wav.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class TestWAV : public CppUnit::TestFixture
320320
{
321321
FileStream stream(copy.fileName().c_str());
322322
stream.seek(0, IOStream::End);
323-
constexpr char garbage[] = "12345678";
323+
constexpr char garbage[] = "\r2345678";
324324
stream.writeBlock(ByteVector(garbage, sizeof(garbage) - 1));
325325
stream.seek(0);
326326
contentsBeforeModification = stream.readBlock(stream.length());

0 commit comments

Comments
 (0)