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