Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discard extra data when it is too large to fit in the extraData buffe… #1044

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion NAudio.Core/Wave/WaveFormats/WaveFormatExtraData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;

// ReSharper disable once CheckNamespace
namespace NAudio.Wave
Expand Down Expand Up @@ -38,7 +39,13 @@ public WaveFormatExtraData(BinaryReader reader)

internal void ReadExtraData(BinaryReader reader)
{
if (this.extraSize > 0)
if (extraSize > extraData.Length)
{
Debug.WriteLine("Extra data is longer than the extraData buffer and has been discarded");
reader.ReadBytes(extraSize);
extraSize = 0;
}
if (extraSize > 0)
{
reader.Read(extraData, 0, extraSize);
}
Expand Down
43 changes: 43 additions & 0 deletions NAudioTests/WaveStreams/WaveFileReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,48 @@ public void Mp3FileReaderDisposesFileOnFailToParse()
File.Delete(tempFilePath);
}
}

[Test]
[Category("UnitTest")]
public void TestLargeFmtChunk()
{
// arrange
var bytes = new List<byte>();
bytes.AddRange(new byte[]
{
0x52, 0x49, 0x46, 0x46, 0xf8, 0xc1, 0x6e, 0x00, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20,
0xe4, 0x01, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x80, 0x3e, 0x00, 0x00, 0x00, 0xfa, 0x00, 0x00,
0x04, 0x00, 0x10

});
for (var i = 0; i < 469; i++)
{
bytes.Add(0x00);
}
bytes.AddRange(new byte[]
{
0x64, 0x61, 0x74, 0x61, 0x00, 0xc0, 0x6e, 0x00
});
for (var i = 0; i < 7258112; i++)
{
bytes.Add(0x00);
}

using (var inputStream = new MemoryStream(bytes.ToArray()))
{
// act
var chunkReader = new WaveFileChunkReader();
chunkReader.ReadWaveHeader(inputStream);

// assert
Assert.AreEqual(64000, chunkReader.WaveFormat.AverageBytesPerSecond);
Assert.AreEqual(16, chunkReader.WaveFormat.BitsPerSample);
Assert.AreEqual(2, chunkReader.WaveFormat.Channels);
Assert.AreEqual(16000, chunkReader.WaveFormat.SampleRate);

Assert.AreEqual(512, chunkReader.DataChunkPosition);
Assert.AreEqual(7258112, chunkReader.DataChunkLength);
}
}
}
}