c604cfc
diff -Nur audiofile-0.3.6/libaudiofile/modules/BlockCodec.cpp audiofile-0.3.6-pull42/libaudiofile/modules/BlockCodec.cpp
c604cfc
--- audiofile-0.3.6/libaudiofile/modules/BlockCodec.cpp	2013-03-06 06:30:03.000000000 +0100
c604cfc
+++ audiofile-0.3.6-pull42/libaudiofile/modules/BlockCodec.cpp	2017-03-10 15:40:02.000000000 +0100
c604cfc
@@ -52,8 +52,9 @@
c604cfc
 	// Decompress into m_outChunk.
c604cfc
 	for (int i=0; i
c604cfc
 	{
c604cfc
-		decodeBlock(static_cast<const uint8_t *>(m_inChunk->buffer) + i * m_bytesPerPacket,
c604cfc
-			static_cast<int16_t *>(m_outChunk->buffer) + i * m_framesPerPacket * m_track->f.channelCount);
c604cfc
+		if (decodeBlock(static_cast<const uint8_t *>(m_inChunk->buffer) + i * m_bytesPerPacket,
c604cfc
+			static_cast<int16_t *>(m_outChunk->buffer) + i * m_framesPerPacket * m_track->f.channelCount)==0)
c604cfc
+			break;
c604cfc
 
c604cfc
 		framesRead += m_framesPerPacket;
c604cfc
 	}
c604cfc
diff -Nur audiofile-0.3.6/libaudiofile/modules/MSADPCM.cpp audiofile-0.3.6-pull42/libaudiofile/modules/MSADPCM.cpp
c604cfc
--- audiofile-0.3.6/libaudiofile/modules/MSADPCM.cpp	2013-03-06 06:30:03.000000000 +0100
c604cfc
+++ audiofile-0.3.6-pull42/libaudiofile/modules/MSADPCM.cpp	2017-03-10 15:40:02.000000000 +0100
c604cfc
@@ -101,24 +101,60 @@
c604cfc
 	768, 614, 512, 409, 307, 230, 230, 230
c604cfc
 };
c604cfc
 
c604cfc
+int firstBitSet(int x)
c604cfc
+{
c604cfc
+        int position=0;
c604cfc
+        while (x!=0)
c604cfc
+        {
c604cfc
+                x>>=1;
c604cfc
+                ++position;
c604cfc
+        }
c604cfc
+        return position;
c604cfc
+}
c604cfc
+
c604cfc
+#ifndef __has_builtin
c604cfc
+#define __has_builtin(x) 0
c604cfc
+#endif
c604cfc
+
c604cfc
+bool multiplyCheckOverflow(int a, int b, int *result)
c604cfc
+{
c604cfc
+#if (defined __GNUC__ && __GNUC__ >= 5) || ( __clang__ && __has_builtin(__builtin_mul_overflow))
c604cfc
+	return __builtin_mul_overflow(a, b, result);
c604cfc
+#else
c604cfc
+	if (firstBitSet(a)+firstBitSet(b)>31) // int is signed, so we can't use 32 bits
c604cfc
+		return true;
c604cfc
+	*result = a * b;
c604cfc
+	return false;
c604cfc
+#endif
c604cfc
+}
c604cfc
+
c604cfc
+
c604cfc
 // Compute a linear PCM value from the given differential coded value.
c604cfc
 static int16_t decodeSample(ms_adpcm_state &state,
c604cfc
-	uint8_t code, const int16_t *coefficient)
c604cfc
+	uint8_t code, const int16_t *coefficient, bool *ok=NULL)
c604cfc
 {
c604cfc
 	int linearSample = (state.sample1 * coefficient[0] +
c604cfc
 		state.sample2 * coefficient[1]) >> 8;
c604cfc
+	int delta;
c604cfc
 
c604cfc
 	linearSample += ((code & 0x08) ? (code - 0x10) : code) * state.delta;
c604cfc
 
c604cfc
 	linearSample = clamp(linearSample, MIN_INT16, MAX_INT16);
c604cfc
 
c604cfc
-	int delta = (state.delta * adaptationTable[code]) >> 8;
c604cfc
+	if (multiplyCheckOverflow(state.delta, adaptationTable[code], &delta))
c604cfc
+	{
c604cfc
+                if (ok) *ok=false;
c604cfc
+		_af_error(AF_BAD_COMPRESSION, "Error decoding sample");
c604cfc
+		return 0;
c604cfc
+	}
c604cfc
+	delta >>= 8;
c604cfc
 	if (delta < 16)
c604cfc
 		delta = 16;
c604cfc
 
c604cfc
 	state.delta = delta;
c604cfc
 	state.sample2 = state.sample1;
c604cfc
 	state.sample1 = linearSample;
c604cfc
+	if (ok) *ok=true;
c604cfc
 
c604cfc
 	return static_cast<int16_t>(linearSample);
c604cfc
 }
c604cfc
@@ -212,13 +248,16 @@
c604cfc
 	{
c604cfc
 		uint8_t code;
c604cfc
 		int16_t newSample;
c604cfc
+		bool ok;
c604cfc
 
c604cfc
 		code = *encoded >> 4;
c604cfc
-		newSample = decodeSample(*state[0], code, coefficient[0]);
c604cfc
+		newSample = decodeSample(*state[0], code, coefficient[0], &ok;;
c604cfc
+		if (!ok) return 0;
c604cfc
 		*decoded++ = newSample;
c604cfc
 
c604cfc
 		code = *encoded & 0x0f;
c604cfc
-		newSample = decodeSample(*state[1], code, coefficient[1]);
c604cfc
+		newSample = decodeSample(*state[1], code, coefficient[1], &ok;;
c604cfc
+		if (!ok) return 0;
c604cfc
 		*decoded++ = newSample;
c604cfc
 
c604cfc
 		encoded++;
c604cfc
diff -Nur audiofile-0.3.6/libaudiofile/WAVE.cpp audiofile-0.3.6-pull42/libaudiofile/WAVE.cpp
c604cfc
--- audiofile-0.3.6/libaudiofile/WAVE.cpp	2013-03-06 06:30:03.000000000 +0100
c604cfc
+++ audiofile-0.3.6-pull42/libaudiofile/WAVE.cpp	2017-03-10 15:40:02.000000000 +0100
c604cfc
@@ -281,6 +281,12 @@
c604cfc
 
c604cfc
 			/* numCoefficients should be at least 7. */
c604cfc
 			assert(numCoefficients >= 7 && numCoefficients <= 255);
c604cfc
+			if (numCoefficients < 7 || numCoefficients > 255)
c604cfc
+			{
c604cfc
+				_af_error(AF_BAD_HEADER,
c604cfc
+						"Bad number of coefficients");
c604cfc
+				return AF_FAIL;
c604cfc
+			}
c604cfc
 
c604cfc
 			m_msadpcmNumCoefficients = numCoefficients;
c604cfc
 
c604cfc
@@ -834,6 +840,8 @@
c604cfc
 	}
c604cfc
 
c604cfc
 	TrackSetup *track = setup->getTrack();
c604cfc
+	if (!track)
c604cfc
+		return AF_NULL_FILESETUP;
c604cfc
 
c604cfc
 	if (track->f.isCompressed())
c604cfc
 	{
c604cfc
diff -Nur audiofile-0.3.6/sfcommands/sfconvert.c audiofile-0.3.6-pull42/sfcommands/sfconvert.c
c604cfc
--- audiofile-0.3.6/sfcommands/sfconvert.c	2013-03-06 06:30:03.000000000 +0100
c604cfc
+++ audiofile-0.3.6-pull42/sfcommands/sfconvert.c	2017-03-10 15:40:02.000000000 +0100
c604cfc
@@ -45,6 +45,33 @@
c604cfc
 void usageerror (void);
c604cfc
 bool copyaudiodata (AFfilehandle infile, AFfilehandle outfile, int trackid);
c604cfc
 
c604cfc
+int firstBitSet(int x)
c604cfc
+{
c604cfc
+        int position=0;
c604cfc
+        while (x!=0)
c604cfc
+        {
c604cfc
+                x>>=1;
c604cfc
+                ++position;
c604cfc
+        }
c604cfc
+        return position;
c604cfc
+}
c604cfc
+
c604cfc
+#ifndef __has_builtin
c604cfc
+#define __has_builtin(x) 0
c604cfc
+#endif
c604cfc
+
c604cfc
+bool multiplyCheckOverflow(int a, int b, int *result)
c604cfc
+{
c604cfc
+#if (defined __GNUC__ && __GNUC__ >= 5) || ( __clang__ && __has_builtin(__builtin_mul_overflow))
c604cfc
+	return __builtin_mul_overflow(a, b, result);
c604cfc
+#else
c604cfc
+	if (firstBitSet(a)+firstBitSet(b)>31) // int is signed, so we can't use 32 bits
c604cfc
+		return true;
c604cfc
+	*result = a * b;
c604cfc
+	return false;
c604cfc
+#endif
c604cfc
+}
c604cfc
+
c604cfc
 int main (int argc, char **argv)
c604cfc
 {
c604cfc
 	if (argc == 2)
c604cfc
@@ -323,8 +350,11 @@
c604cfc
 {
c604cfc
 	int frameSize = afGetVirtualFrameSize(infile, trackid, 1);
c604cfc
 
c604cfc
-	const int kBufferFrameCount = 65536;
c604cfc
-	void *buffer = malloc(kBufferFrameCount * frameSize);
c604cfc
+	int kBufferFrameCount = 65536;
c604cfc
+	int bufferSize;
c604cfc
+	while (multiplyCheckOverflow(kBufferFrameCount, frameSize, &bufferSize))
c604cfc
+		kBufferFrameCount /= 2;
c604cfc
+	void *buffer = malloc(bufferSize);
c604cfc
 
c604cfc
 	AFframecount totalFrames = afGetFrameCount(infile, AF_DEFAULT_TRACK);
c604cfc
 	AFframecount totalFramesWritten = 0;