From bc90539040015fe8014a469f2c10b79378f68bd7 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Mon, 5 Sep 2016 08:57:07 +0300 Subject: [PATCH 1/2] fix initialization of local blockAlign with default values. Set default values to local `blockAlign` before asigning the value to the member field `this.blockAlign`. fix #791 --- Source/SharpDX/Multimedia/WaveFormatAdpcm.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Source/SharpDX/Multimedia/WaveFormatAdpcm.cs b/Source/SharpDX/Multimedia/WaveFormatAdpcm.cs index a50c57bb7..598dfc5a7 100644 --- a/Source/SharpDX/Multimedia/WaveFormatAdpcm.cs +++ b/Source/SharpDX/Multimedia/WaveFormatAdpcm.cs @@ -46,25 +46,19 @@ internal WaveFormatAdpcm() /// The block align. If 0, then 256 for [0, 11KHz], 512 for ]11KHz, 22Khz], 1024 for ]22Khz, +inf] public WaveFormatAdpcm(int rate, int channels, int blockAlign = 0) : base(rate, 4, channels) { - waveFormatTag = WaveFormatEncoding.Adpcm; - this.blockAlign = (short)blockAlign; - if (blockAlign == 0) { if (rate <= 11025) - { blockAlign = 256; - } else if (rate <= 22050) - { blockAlign = 512; - } else - { blockAlign = 1024; - } } + waveFormatTag = WaveFormatEncoding.Adpcm; + this.blockAlign = (short)blockAlign; + SamplesPerBlock = (ushort)(blockAlign * 2 / channels - 12); averageBytesPerSecond = (SampleRate * blockAlign) / SamplesPerBlock; From 1e41b3fbd42f5f6dc2f70ef6755dbd869521a554 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Mon, 5 Sep 2016 09:07:39 +0300 Subject: [PATCH 2/2] Validate blockAlign can be cast to short --- Source/SharpDX/Multimedia/WaveFormatAdpcm.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Source/SharpDX/Multimedia/WaveFormatAdpcm.cs b/Source/SharpDX/Multimedia/WaveFormatAdpcm.cs index 598dfc5a7..76c2207bb 100644 --- a/Source/SharpDX/Multimedia/WaveFormatAdpcm.cs +++ b/Source/SharpDX/Multimedia/WaveFormatAdpcm.cs @@ -56,6 +56,11 @@ public WaveFormatAdpcm(int rate, int channels, int blockAlign = 0) : base(rate, blockAlign = 1024; } + if (rate <= 0) throw new ArgumentOutOfRangeException("rate", "Must be > 0"); + if (channels <= 0) throw new ArgumentOutOfRangeException("channels", "Must be > 0"); + if (blockAlign <= 0) throw new ArgumentOutOfRangeException("blockAlign", "Must be > 0"); + if (blockAlign > Int16.MaxValue) throw new ArgumentOutOfRangeException("blockAlign", "Must be < 32767"); + waveFormatTag = WaveFormatEncoding.Adpcm; this.blockAlign = (short)blockAlign;