ubmitted By: Bruce Dubbs <bdubbs@linuxfromscratch.org>
Date: 2021-12-12
Initial Package Version: 0.3.6
Upstream Status: Not incorproated.  Package appears to be unmaintained.
Origin: Arch Linux
URL: https://github.com/archlinux/svntogit-packages/tree/packages/audiofile/trunk
Description: Consolidates several patches including several CVE fixes

diff -Naur audiofile-0.3.6.orig/libaudiofile/modules/BlockCodec.cpp audiofile-0.3.6/libaudiofile/modules/BlockCodec.cpp
--- audiofile-0.3.6.orig/libaudiofile/modules/BlockCodec.cpp	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/libaudiofile/modules/BlockCodec.cpp	2021-12-13 00:09:48.429627396 -0600
@@ -47,13 +47,14 @@
 
 	// Read the compressed data.
 	ssize_t bytesRead = read(m_inChunk->buffer, m_bytesPerPacket * blockCount);
-	int blocksRead = bytesRead >= 0 ? bytesRead / m_bytesPerPacket : 0;
+	int blocksRead = (bytesRead >= 0 && m_bytesPerPacket > 0) ? bytesRead / m_bytesPerPacket : 0;
 
 	// Decompress into m_outChunk.
 	for (int i=0; i<blocksRead; i++)
 	{
-		decodeBlock(static_cast<const uint8_t *>(m_inChunk->buffer) + i * m_bytesPerPacket,
-			static_cast<int16_t *>(m_outChunk->buffer) + i * m_framesPerPacket * m_track->f.channelCount);
+		if (decodeBlock(static_cast<const uint8_t *>(m_inChunk->buffer) + i * m_bytesPerPacket,
+			static_cast<int16_t *>(m_outChunk->buffer) + i * m_framesPerPacket * m_track->f.channelCount)==0)
+			break;
 
 		framesRead += m_framesPerPacket;
 	}
diff -Naur audiofile-0.3.6.orig/libaudiofile/modules/IMA.cpp audiofile-0.3.6/libaudiofile/modules/IMA.cpp
--- audiofile-0.3.6.orig/libaudiofile/modules/IMA.cpp	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/libaudiofile/modules/IMA.cpp	2021-12-13 00:08:34.205889024 -0600
@@ -169,7 +169,7 @@
 		if (encoded[1] & 0x80)
 			m_adpcmState[c].previousValue -= 0x10000;
 
-		m_adpcmState[c].index = encoded[2];
+		m_adpcmState[c].index = clamp(encoded[2], 0, 88);
 
 		*decoded++ = m_adpcmState[c].previousValue;
 
@@ -210,7 +210,7 @@
 			predictor -= 0x10000;
 
 		state.previousValue = clamp(predictor, MIN_INT16, MAX_INT16);
-		state.index = encoded[1] & 0x7f;
+		state.index = clamp(encoded[1] & 0x7f, 0, 88);
 		encoded += 2;
 
 		for (int n=0; n<m_framesPerPacket; n+=2)
diff -Naur audiofile-0.3.6.orig/libaudiofile/modules/ModuleState.cpp audiofile-0.3.6/libaudiofile/modules/ModuleState.cpp
--- audiofile-0.3.6.orig/libaudiofile/modules/ModuleState.cpp	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/libaudiofile/modules/ModuleState.cpp	2021-12-13 00:09:55.710699805 -0600
@@ -75,6 +75,9 @@
 		m_fileModule = unit->initcompress(track, file->m_fh, file->m_seekok,
 			file->m_fileFormat == AF_FILE_RAWDATA, &chunkFrames);
 
+	if (!m_fileModule)
+		return AF_FAIL;
+
 	if (unit->needsRebuffer)
 	{
 		assert(unit->nativeSampleFormat == AF_SAMPFMT_TWOSCOMP);
@@ -402,7 +405,7 @@
 		addModule(new Transform(outfc, in.pcm, out.pcm));
 
 	if (in.channelCount != out.channelCount)
-		addModule(new ApplyChannelMatrix(infc, isReading,
+		addModule(new ApplyChannelMatrix(outfc, isReading,
 			in.channelCount, out.channelCount,
 			in.pcm.minClip, in.pcm.maxClip,
 			track->channelMatrix));
diff -Naur audiofile-0.3.6.orig/libaudiofile/modules/MSADPCM.cpp audiofile-0.3.6/libaudiofile/modules/MSADPCM.cpp
--- audiofile-0.3.6.orig/libaudiofile/modules/MSADPCM.cpp	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/libaudiofile/modules/MSADPCM.cpp	2021-12-13 00:09:31.070454742 -0600
@@ -101,24 +101,60 @@
 	768, 614, 512, 409, 307, 230, 230, 230
 };
 
+int firstBitSet(int x)
+{
+        int position=0;
+        while (x!=0)
+        {
+                x>>=1;
+                ++position;
+        }
+        return position;
+}
+
+#ifndef __has_builtin
+#define __has_builtin(x) 0
+#endif
+
+bool multiplyCheckOverflow(int a, int b, int *result)
+{
+#if (defined __GNUC__ && __GNUC__ >= 5) || ( __clang__ && __has_builtin(__builtin_mul_overflow))
+	return __builtin_mul_overflow(a, b, result);
+#else
+	if (firstBitSet(a)+firstBitSet(b)>31) // int is signed, so we can't use 32 bits
+		return true;
+	*result = a * b;
+	return false;
+#endif
+}
+
+
 // Compute a linear PCM value from the given differential coded value.
 static int16_t decodeSample(ms_adpcm_state &state,
-	uint8_t code, const int16_t *coefficient)
+	uint8_t code, const int16_t *coefficient, bool *ok=NULL)
 {
 	int linearSample = (state.sample1 * coefficient[0] +
 		state.sample2 * coefficient[1]) >> 8;
+	int delta;
 
 	linearSample += ((code & 0x08) ? (code - 0x10) : code) * state.delta;
 
 	linearSample = clamp(linearSample, MIN_INT16, MAX_INT16);
 
-	int delta = (state.delta * adaptationTable[code]) >> 8;
+	if (multiplyCheckOverflow(state.delta, adaptationTable[code], &delta))
+	{
+                if (ok) *ok=false;
+		_af_error(AF_BAD_COMPRESSION, "Error decoding sample");
+		return 0;
+	}
+	delta >>= 8;
 	if (delta < 16)
 		delta = 16;
 
 	state.delta = delta;
 	state.sample2 = state.sample1;
 	state.sample1 = linearSample;
+	if (ok) *ok=true;
 
 	return static_cast<int16_t>(linearSample);
 }
@@ -212,13 +248,16 @@
 	{
 		uint8_t code;
 		int16_t newSample;
+		bool ok;
 
 		code = *encoded >> 4;
-		newSample = decodeSample(*state[0], code, coefficient[0]);
+		newSample = decodeSample(*state[0], code, coefficient[0], &ok);
+		if (!ok) return 0;
 		*decoded++ = newSample;
 
 		code = *encoded & 0x0f;
-		newSample = decodeSample(*state[1], code, coefficient[1]);
+		newSample = decodeSample(*state[1], code, coefficient[1], &ok);
+		if (!ok) return 0;
 		*decoded++ = newSample;
 
 		encoded++;
diff -Naur audiofile-0.3.6.orig/libaudiofile/modules/SimpleModule.cpp audiofile-0.3.6/libaudiofile/modules/SimpleModule.cpp
--- audiofile-0.3.6.orig/libaudiofile/modules/SimpleModule.cpp	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/libaudiofile/modules/SimpleModule.cpp	2021-12-13 00:10:01.326755642 -0600
@@ -26,6 +26,7 @@
 void SimpleModule::runPull()
 {
 	pull(m_outChunk->frameCount);
+	m_outChunk->frameCount = m_inChunk->frameCount;
 	run(*m_inChunk, *m_outChunk);
 }
 
diff -Naur audiofile-0.3.6.orig/libaudiofile/modules/SimpleModule.h audiofile-0.3.6/libaudiofile/modules/SimpleModule.h
--- audiofile-0.3.6.orig/libaudiofile/modules/SimpleModule.h	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/libaudiofile/modules/SimpleModule.h	2021-12-13 00:08:12.589673905 -0600
@@ -123,7 +123,7 @@
 	typedef typename IntTypes<Format>::UnsignedType UnsignedType;
 
 	static const int kScaleBits = (Format + 1) * CHAR_BIT - 1;
-	static const int kMinSignedValue = -1 << kScaleBits;
+	static const int kMinSignedValue = 0-(1U<<kScaleBits);
 
 	struct signedToUnsigned : public std::unary_function<SignedType, UnsignedType>
 	{
diff -Naur audiofile-0.3.6.orig/libaudiofile/WAVE.cpp audiofile-0.3.6/libaudiofile/WAVE.cpp
--- audiofile-0.3.6.orig/libaudiofile/WAVE.cpp	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/libaudiofile/WAVE.cpp	2021-12-13 00:09:38.782531453 -0600
@@ -281,6 +281,12 @@
 
 			/* numCoefficients should be at least 7. */
 			assert(numCoefficients >= 7 && numCoefficients <= 255);
+			if (numCoefficients < 7 || numCoefficients > 255)
+			{
+				_af_error(AF_BAD_HEADER,
+						"Bad number of coefficients");
+				return AF_FAIL;
+			}
 
 			m_msadpcmNumCoefficients = numCoefficients;
 
@@ -326,6 +332,7 @@
 			{
 				_af_error(AF_BAD_NOT_IMPLEMENTED,
 					"IMA ADPCM compression supports only 4 bits per sample");
+				return AF_FAIL;
 			}
 
 			int bytesPerBlock = (samplesPerBlock + 14) / 8 * 4 * channelCount;
@@ -333,6 +340,7 @@
 			{
 				_af_error(AF_BAD_CODEC_CONFIG,
 					"Invalid samples per block for IMA ADPCM compression");
+				return AF_FAIL;
 			}
 
 			track->f.sampleWidth = 16;
diff -Naur audiofile-0.3.6.orig/sfcommands/sfconvert.c audiofile-0.3.6/sfcommands/sfconvert.c
--- audiofile-0.3.6.orig/sfcommands/sfconvert.c	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/sfcommands/sfconvert.c	2021-12-13 00:09:31.070454742 -0600
@@ -45,6 +45,33 @@
 void usageerror (void);
 bool copyaudiodata (AFfilehandle infile, AFfilehandle outfile, int trackid);
 
+int firstBitSet(int x)
+{
+        int position=0;
+        while (x!=0)
+        {
+                x>>=1;
+                ++position;
+        }
+        return position;
+}
+
+#ifndef __has_builtin
+#define __has_builtin(x) 0
+#endif
+
+bool multiplyCheckOverflow(int a, int b, int *result)
+{
+#if (defined __GNUC__ && __GNUC__ >= 5) || ( __clang__ && __has_builtin(__builtin_mul_overflow))
+	return __builtin_mul_overflow(a, b, result);
+#else
+	if (firstBitSet(a)+firstBitSet(b)>31) // int is signed, so we can't use 32 bits
+		return true;
+	*result = a * b;
+	return false;
+#endif
+}
+
 int main (int argc, char **argv)
 {
 	if (argc == 2)
@@ -323,8 +350,11 @@
 {
 	int frameSize = afGetVirtualFrameSize(infile, trackid, 1);
 
-	const int kBufferFrameCount = 65536;
-	void *buffer = malloc(kBufferFrameCount * frameSize);
+	int kBufferFrameCount = 65536;
+	int bufferSize;
+	while (multiplyCheckOverflow(kBufferFrameCount, frameSize, &bufferSize))
+		kBufferFrameCount /= 2;
+	void *buffer = malloc(bufferSize);
 
 	AFframecount totalFrames = afGetFrameCount(infile, AF_DEFAULT_TRACK);
 	AFframecount totalFramesWritten = 0;
diff -Naur audiofile-0.3.6.orig/test/floatto24.c audiofile-0.3.6/test/floatto24.c
--- audiofile-0.3.6.orig/test/floatto24.c	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/floatto24.c	2021-12-13 00:08:20.493752571 -0600
@@ -86,8 +86,8 @@
 	afInitChannels(setup, AF_DEFAULT_TRACK, 1);
 	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_FLOAT, 32);
 
-	char testFileName[PATH_MAX];
-	if (!createTemporaryFile("floatto24", testFileName))
+	char *testFileName;
+	if (!createTemporaryFile("floatto24", &testFileName))
 	{
 		fprintf(stderr, "Could not create temporary file.\n");
 		exit(EXIT_FAILURE);
@@ -182,6 +182,7 @@
 	}
 
 	unlink(testFileName);
+	free(testFileName);
 
 	exit(EXIT_SUCCESS);
 }
diff -Naur audiofile-0.3.6.orig/test/FloatToInt.cpp audiofile-0.3.6/test/FloatToInt.cpp
--- audiofile-0.3.6.orig/test/FloatToInt.cpp	2013-02-11 11:23:26.000000000 -0600
+++ audiofile-0.3.6/test/FloatToInt.cpp	2021-12-13 00:08:12.589673905 -0600
@@ -115,7 +115,7 @@
 		EXPECT_EQ(readData[i], expectedData[i]);
 }
 
-static const int32_t kMinInt24 = -1<<23;
+static const int32_t kMinInt24 = 0-(1U<<23);
 static const int32_t kMaxInt24 = (1<<23) - 1;
 
 TEST_F(FloatToIntTest, Int24)
diff -Naur audiofile-0.3.6.orig/test/IntToFloat.cpp audiofile-0.3.6/test/IntToFloat.cpp
--- audiofile-0.3.6.orig/test/IntToFloat.cpp	2013-02-11 11:23:26.000000000 -0600
+++ audiofile-0.3.6/test/IntToFloat.cpp	2021-12-13 00:08:12.589673905 -0600
@@ -117,7 +117,7 @@
 		EXPECT_EQ(readData[i], expectedData[i]);
 }
 
-static const int32_t kMinInt24 = -1<<23;
+static const int32_t kMinInt24 = 0-(1U<<23);
 static const int32_t kMaxInt24 = (1<<23) - 1;
 
 TEST_F(IntToFloatTest, Int24)
diff -Naur audiofile-0.3.6.orig/test/Makefile.am audiofile-0.3.6/test/Makefile.am
--- audiofile-0.3.6.orig/test/Makefile.am	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/Makefile.am	2021-12-13 00:08:28.061827885 -0600
@@ -26,6 +26,7 @@
 	VirtualFile \
 	floatto24 \
 	query2 \
+	sixteen-stereo-to-eight-mono \
 	sixteen-to-eight \
 	testchannelmatrix \
 	testdouble \
@@ -139,6 +140,7 @@
 printmarkers_LDADD = $(LIBAUDIOFILE) -lm
 
 sixteen_to_eight_SOURCES = sixteen-to-eight.c TestUtilities.cpp TestUtilities.h
+sixteen_stereo_to_eight_mono_SOURCES = sixteen-stereo-to-eight-mono.c TestUtilities.cpp TestUtilities.h
 
 testchannelmatrix_SOURCES = testchannelmatrix.c TestUtilities.cpp TestUtilities.h
 
diff -Naur audiofile-0.3.6.orig/test/NeXT.cpp audiofile-0.3.6/test/NeXT.cpp
--- audiofile-0.3.6.orig/test/NeXT.cpp	2013-02-11 11:23:26.000000000 -0600
+++ audiofile-0.3.6/test/NeXT.cpp	2021-12-13 00:08:12.590673915 -0600
@@ -37,13 +37,13 @@
 
 #include "TestUtilities.h"
 
-const char kDataUnspecifiedLength[] =
+const signed char kDataUnspecifiedLength[] =
 {
 	'.', 's', 'n', 'd',
 	0, 0, 0, 24, // offset of 24 bytes
-	0xff, 0xff, 0xff, 0xff, // unspecified length
+	-1, -1, -1, -1, // unspecified length
 	0, 0, 0, 3, // 16-bit linear
-	0, 0, 172, 68, // 44100 Hz
+	0, 0, -84, 68, // 44100 Hz (0xAC44)
 	0, 0, 0, 1, // 1 channel
 	0, 1,
 	0, 1,
@@ -57,13 +57,13 @@
 	0, 55
 };
 
-const char kDataTruncated[] =
+const signed char kDataTruncated[] =
 {
 	'.', 's', 'n', 'd',
 	0, 0, 0, 24, // offset of 24 bytes
 	0, 0, 0, 20, // length of 20 bytes
 	0, 0, 0, 3, // 16-bit linear
-	0, 0, 172, 68, // 44100 Hz
+	0, 0, -84, 68, // 44100 Hz (0xAC44)
 	0, 0, 0, 1, // 1 channel
 	0, 1,
 	0, 1,
@@ -152,13 +152,13 @@
 	ASSERT_EQ(::unlink(testFileName.c_str()), 0);
 }
 
-const char kDataZeroChannels[] =
+const signed char kDataZeroChannels[] =
 {
 	'.', 's', 'n', 'd',
 	0, 0, 0, 24, // offset of 24 bytes
 	0, 0, 0, 2, // 2 bytes
 	0, 0, 0, 3, // 16-bit linear
-	0, 0, 172, 68, // 44100 Hz
+	0, 0, -84, 68, // 44100 Hz (0xAC44)
 	0, 0, 0, 0, // 0 channels
 	0, 1
 };
diff -Naur audiofile-0.3.6.orig/test/Sign.cpp audiofile-0.3.6/test/Sign.cpp
--- audiofile-0.3.6.orig/test/Sign.cpp	2013-02-11 11:23:26.000000000 -0600
+++ audiofile-0.3.6/test/Sign.cpp	2021-12-13 00:08:12.590673915 -0600
@@ -116,7 +116,7 @@
 		EXPECT_EQ(readData[i], expectedData[i]);
 }
 
-static const int32_t kMinInt24 = -1<<23;
+static const int32_t kMinInt24 = 0-(1U<<23);
 static const int32_t kMaxInt24 = (1<<23) - 1;
 static const uint32_t kMaxUInt24 = (1<<24) - 1;
 
diff -Naur audiofile-0.3.6.orig/test/sixteen-stereo-to-eight-mono.c audiofile-0.3.6/test/sixteen-stereo-to-eight-mono.c
--- audiofile-0.3.6.orig/test/sixteen-stereo-to-eight-mono.c	1969-12-31 18:00:00.000000000 -0600
+++ audiofile-0.3.6/test/sixteen-stereo-to-eight-mono.c	2021-12-13 00:08:28.061827885 -0600
@@ -0,0 +1,118 @@
+/*
+	Audio File Library
+
+	Copyright 2000, Silicon Graphics, Inc.
+
+	This program is free software; you can redistribute it and/or modify
+	it under the terms of the GNU General Public License as published by
+	the Free Software Foundation; either version 2 of the License, or
+	(at your option) any later version.
+
+	This program is distributed in the hope that it will be useful,
+	but WITHOUT ANY WARRANTY; without even the implied warranty of
+	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+	GNU General Public License for more details.
+
+	You should have received a copy of the GNU General Public License along
+	with this program; if not, write to the Free Software Foundation, Inc.,
+	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+/*
+	sixteen-stereo-to-eight-mono.c
+
+	This program tests the conversion from 2-channel 16-bit integers to
+	1-channel 8-bit	integers.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <limits.h>
+
+#include <audiofile.h>
+
+#include "TestUtilities.h"
+
+int main (int argc, char **argv)
+{
+	AFfilehandle file;
+	AFfilesetup setup;
+	int16_t frames16[] = {14298, 392, 3923, -683, 958, -1921};
+	int8_t frames8[] = {28, 6, -2};
+	int i, frameCount = 3;
+	int8_t byte;
+	AFframecount result;
+
+	setup = afNewFileSetup();
+
+	afInitFileFormat(setup, AF_FILE_WAVE);
+
+	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+	afInitChannels(setup, AF_DEFAULT_TRACK, 2);
+
+	char *testFileName;
+	if (!createTemporaryFile("sixteen-to-eight", &testFileName))
+	{
+		fprintf(stderr, "Could not create temporary file.\n");
+		exit(EXIT_FAILURE);
+	}
+
+	file = afOpenFile(testFileName, "w", setup);
+	if (file == AF_NULL_FILEHANDLE)
+	{
+		fprintf(stderr, "could not open file for writing\n");
+		exit(EXIT_FAILURE);
+	}
+
+	afFreeFileSetup(setup);
+
+	afWriteFrames(file, AF_DEFAULT_TRACK, frames16, frameCount);
+
+	afCloseFile(file);
+
+	file = afOpenFile(testFileName, "r", AF_NULL_FILESETUP);
+	if (file == AF_NULL_FILEHANDLE)
+	{
+		fprintf(stderr, "could not open file for reading\n");
+		exit(EXIT_FAILURE);
+	}
+
+	afSetVirtualSampleFormat(file, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 8);
+	afSetVirtualChannels(file, AF_DEFAULT_TRACK, 1);
+
+	for (i=0; i<frameCount; i++)
+	{
+		/* Read one frame. */
+		result = afReadFrames(file, AF_DEFAULT_TRACK, &byte, 1);
+
+		if (result != 1)
+			break;
+
+		/* Compare the byte read with its precalculated value. */
+		if (memcmp(&byte, &frames8[i], 1) != 0)
+		{
+			printf("error\n");
+			printf("expected %d, got %d\n", frames8[i], byte);
+			exit(EXIT_FAILURE);
+		}
+		else
+		{
+#ifdef DEBUG
+			printf("got what was expected: %d\n", byte);
+#endif
+		}
+	}
+
+	afCloseFile(file);
+	unlink(testFileName);
+	free(testFileName);
+
+	exit(EXIT_SUCCESS);
+}
diff -Naur audiofile-0.3.6.orig/test/sixteen-to-eight.c audiofile-0.3.6/test/sixteen-to-eight.c
--- audiofile-0.3.6.orig/test/sixteen-to-eight.c	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/sixteen-to-eight.c	2021-12-13 00:08:20.494752581 -0600
@@ -57,8 +57,8 @@
 	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_UNSIGNED, 8);
 	afInitChannels(setup, AF_DEFAULT_TRACK, 1);
 
-	char testFileName[PATH_MAX];
-	if (!createTemporaryFile("sixteen-to-eight", testFileName))
+	char *testFileName;
+	if (!createTemporaryFile("sixteen-to-eight", &testFileName))
 	{
 		fprintf(stderr, "Could not create temporary file.\n");
 		exit(EXIT_FAILURE);
@@ -113,6 +113,7 @@
 
 	afCloseFile(file);
 	unlink(testFileName);
+	free(testFileName);
 
 	exit(EXIT_SUCCESS);
 }
diff -Naur audiofile-0.3.6.orig/test/testchannelmatrix.c audiofile-0.3.6/test/testchannelmatrix.c
--- audiofile-0.3.6.orig/test/testchannelmatrix.c	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/testchannelmatrix.c	2021-12-13 00:08:20.494752581 -0600
@@ -39,7 +39,7 @@
 
 #include "TestUtilities.h"
 
-static char sTestFileName[PATH_MAX];
+static char *sTestFileName;
 
 const short samples[] = {300, -300, 515, -515, 2315, -2315, 9154, -9154};
 #define SAMPLE_COUNT (sizeof (samples) / sizeof (short))
@@ -47,7 +47,11 @@
 
 void cleanup (void)
 {
-	unlink(sTestFileName);
+	if (sTestFileName)
+	{
+		unlink(sTestFileName);
+		free(sTestFileName);
+	}
 }
 
 void ensure (int condition, const char *message)
@@ -76,7 +80,7 @@
 	afInitFileFormat(setup, AF_FILE_AIFFC);
 
 	/* Write stereo data to test file. */
-	ensure(createTemporaryFile("testchannelmatrix", sTestFileName),
+	ensure(createTemporaryFile("testchannelmatrix", &sTestFileName),
 		"could not create temporary file");
 	file = afOpenFile(sTestFileName, "w", setup);
 	ensure(file != AF_NULL_FILEHANDLE, "could not open file for writing");
diff -Naur audiofile-0.3.6.orig/test/testdouble.c audiofile-0.3.6/test/testdouble.c
--- audiofile-0.3.6.orig/test/testdouble.c	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/testdouble.c	2021-12-13 00:08:20.494752581 -0600
@@ -38,7 +38,7 @@
 
 #include "TestUtilities.h"
 
-static char sTestFileName[PATH_MAX];
+static char *sTestFileName;
 
 const double samples[] =
 	{1.0, 0.6, -0.3, 0.95, 0.2, -0.6, 0.9, 0.4, -0.22, 0.125, 0.1, -0.4};
@@ -48,7 +48,11 @@
 
 void cleanup (void)
 {
-	unlink(sTestFileName);
+	if (sTestFileName)
+	{
+		unlink(sTestFileName);
+		free(sTestFileName);
+	}
 }
 
 void ensure (int condition, const char *message)
@@ -96,7 +100,7 @@
 	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_DOUBLE, 64);
 	afInitChannels(setup, AF_DEFAULT_TRACK, 2);
 
-	ensure(createTemporaryFile("testdouble", sTestFileName),
+	ensure(createTemporaryFile("testdouble", &sTestFileName),
 		"could not create temporary file");
 	file = afOpenFile(sTestFileName, "w", setup);
 	ensure(file != AF_NULL_FILEHANDLE, "could not open file for writing");
diff -Naur audiofile-0.3.6.orig/test/testfloat.c audiofile-0.3.6/test/testfloat.c
--- audiofile-0.3.6.orig/test/testfloat.c	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/testfloat.c	2021-12-13 00:08:20.494752581 -0600
@@ -38,7 +38,7 @@
 
 #include "TestUtilities.h"
 
-static char sTestFileName[PATH_MAX];
+static char *sTestFileName;
 
 const float samples[] =
 	{1.0, 0.6, -0.3, 0.95, 0.2, -0.6, 0.9, 0.4, -0.22, 0.125, 0.1, -0.4};
@@ -48,7 +48,11 @@
 
 void cleanup (void)
 {
-	unlink(sTestFileName);
+	if (sTestFileName)
+	{
+		unlink(sTestFileName);
+		free(sTestFileName);
+	}
 }
 
 void ensure (int condition, const char *message)
@@ -96,7 +100,7 @@
 	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_FLOAT, 32);
 	afInitChannels(setup, AF_DEFAULT_TRACK, 2);
 
-	ensure(createTemporaryFile("testfloat", sTestFileName),
+	ensure(createTemporaryFile("testfloat", &sTestFileName),
 		"could not create temporary file");
 	file = afOpenFile(sTestFileName, "w", setup);
 	ensure(file != AF_NULL_FILEHANDLE, "could not open file for writing");
diff -Naur audiofile-0.3.6.orig/test/testmarkers.c audiofile-0.3.6/test/testmarkers.c
--- audiofile-0.3.6.orig/test/testmarkers.c	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/testmarkers.c	2021-12-13 00:08:20.495752591 -0600
@@ -32,15 +32,19 @@
 
 #include "TestUtilities.h"
 
-static char sTestFileName[PATH_MAX];
+static char *sTestFileName;
 
 #define FRAME_COUNT 200
 
 void cleanup (void)
 {
+	if (sTestFileName)
+	{
 #ifndef DEBUG
-	unlink(sTestFileName);
+		unlink(sTestFileName);
 #endif
+		free(sTestFileName);
+	}
 }
 
 void ensure (int condition, const char *message)
@@ -127,7 +131,7 @@
 
 int main (void)
 {
-	ensure(createTemporaryFile("testmarkers", sTestFileName),
+	ensure(createTemporaryFile("testmarkers", &sTestFileName),
 		"could not create temporary file");
 
 	testmarkers(AF_FILE_AIFF);
diff -Naur audiofile-0.3.6.orig/test/TestUtilities.cpp audiofile-0.3.6/test/TestUtilities.cpp
--- audiofile-0.3.6.orig/test/TestUtilities.cpp	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/TestUtilities.cpp	2021-12-13 00:08:20.493752571 -0600
@@ -21,8 +21,8 @@
 #include "TestUtilities.h"
 
 #include <limits.h>
-#include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
 bool createTemporaryFile(const std::string &prefix, std::string *path)
@@ -35,12 +35,12 @@
 	return true;
 }
 
-bool createTemporaryFile(const char *prefix, char *path)
+bool createTemporaryFile(const char *prefix, char **path)
 {
-	snprintf(path, PATH_MAX, "/tmp/%s-XXXXXX", prefix);
-	int fd = ::mkstemp(path);
-	if (fd < 0)
-		return false;
-	::close(fd);
-	return true;
+	*path = NULL;
+	std::string pathString;
+	bool result = createTemporaryFile(prefix, &pathString);
+	if (result)
+		*path = ::strdup(pathString.c_str());
+	return result;
 }
diff -Naur audiofile-0.3.6.orig/test/TestUtilities.h audiofile-0.3.6/test/TestUtilities.h
--- audiofile-0.3.6.orig/test/TestUtilities.h	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/TestUtilities.h	2021-12-13 00:08:20.493752571 -0600
@@ -53,7 +53,7 @@
 
 #include <stdbool.h>
 
-bool createTemporaryFile(const char *prefix, char *path);
+bool createTemporaryFile(const char *prefix, char **path);
 
 #ifdef __cplusplus
 }
diff -Naur audiofile-0.3.6.orig/test/twentyfour2.c audiofile-0.3.6/test/twentyfour2.c
--- audiofile-0.3.6.orig/test/twentyfour2.c	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/twentyfour2.c	2021-12-13 00:08:20.495752591 -0600
@@ -45,15 +45,19 @@
 
 #include "TestUtilities.h"
 
-static char sTestFileName[PATH_MAX];
+static char *sTestFileName;
 
 #define FRAME_COUNT 10000
 
 void cleanup (void)
 {
+	if (sTestFileName)
+	{
 #ifndef DEBUG
-	unlink(sTestFileName);
+		unlink(sTestFileName);
 #endif
+		free(sTestFileName);
+	}
 }
 
 void ensure (int condition, const char *message)
@@ -78,7 +82,7 @@
 	afInitChannels(setup, AF_DEFAULT_TRACK, 1);
 	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 24);
 
-	ensure(createTemporaryFile("twentyfour2", sTestFileName),
+	ensure(createTemporaryFile("twentyfour2", &sTestFileName),
 		"could not create temporary file");
 	file = afOpenFile(sTestFileName, "w", setup);
 	ensure(file != NULL, "could not open test file for writing");
diff -Naur audiofile-0.3.6.orig/test/twentyfour.c audiofile-0.3.6/test/twentyfour.c
--- audiofile-0.3.6.orig/test/twentyfour.c	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/twentyfour.c	2021-12-13 00:08:20.495752591 -0600
@@ -71,8 +71,8 @@
 	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 24);
 	afInitChannels(setup, AF_DEFAULT_TRACK, 1);
 
-	char testFileName[PATH_MAX];
-	if (!createTemporaryFile("twentyfour", testFileName))
+	char *testFileName;
+	if (!createTemporaryFile("twentyfour", &testFileName))
 	{
 		fprintf(stderr, "could not create temporary file\n");
 		exit(EXIT_FAILURE);
@@ -239,6 +239,7 @@
 		exit(EXIT_FAILURE);
 	}
 	unlink(testFileName);
+	free(testFileName);
 
 	exit(EXIT_SUCCESS);
 }
diff -Naur audiofile-0.3.6.orig/test/writealaw.c audiofile-0.3.6/test/writealaw.c
--- audiofile-0.3.6.orig/test/writealaw.c	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/writealaw.c	2021-12-13 00:08:20.496752601 -0600
@@ -53,7 +53,7 @@
 
 #include "TestUtilities.h"
 
-static char sTestFileName[PATH_MAX];
+static char *sTestFileName;
 
 #define FRAME_COUNT 16
 #define SAMPLE_COUNT FRAME_COUNT
@@ -62,9 +62,13 @@
 
 void cleanup (void)
 {
+	if (sTestFileName)
+	{
 #ifndef DEBUG
-	unlink(sTestFileName);
+		unlink(sTestFileName);
 #endif
+		free(sTestFileName);
+	}
 }
 
 void ensure (int condition, const char *message)
@@ -113,7 +117,7 @@
 	afInitFileFormat(setup, fileFormat);
 	afInitChannels(setup, AF_DEFAULT_TRACK, 1);
 
-	ensure(createTemporaryFile("writealaw", sTestFileName),
+	ensure(createTemporaryFile("writealaw", &sTestFileName),
 		"could not create temporary file");
 	file = afOpenFile(sTestFileName, "w", setup);
 	afFreeFileSetup(setup);
diff -Naur audiofile-0.3.6.orig/test/writeraw.c audiofile-0.3.6/test/writeraw.c
--- audiofile-0.3.6.orig/test/writeraw.c	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/writeraw.c	2021-12-13 00:08:20.496752601 -0600
@@ -44,13 +44,17 @@
 
 #include "TestUtilities.h"
 
-static char sTestFileName[PATH_MAX];
+static char *sTestFileName;
 
 void cleanup (void)
 {
+	if (sTestFileName)
+	{
 #ifndef DEBUG
-	unlink(sTestFileName);
+		unlink(sTestFileName);
 #endif
+		free(sTestFileName);
+	}
 }
 
 void ensure (int condition, const char *message)
@@ -84,7 +88,7 @@
 	afInitChannels(setup, AF_DEFAULT_TRACK, 1);
 	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
 
-	ensure(createTemporaryFile("writeraw", sTestFileName),
+	ensure(createTemporaryFile("writeraw", &sTestFileName),
 		"could not create temporary file");
 	file = afOpenFile(sTestFileName, "w", setup);
 	ensure(file != AF_NULL_FILEHANDLE, "unable to open file for writing");
diff -Naur audiofile-0.3.6.orig/test/writeulaw.c audiofile-0.3.6/test/writeulaw.c
--- audiofile-0.3.6.orig/test/writeulaw.c	2013-03-05 23:30:03.000000000 -0600
+++ audiofile-0.3.6/test/writeulaw.c	2021-12-13 00:08:20.496752601 -0600
@@ -53,7 +53,7 @@
 
 #include "TestUtilities.h"
 
-static char sTestFileName[PATH_MAX];
+static char *sTestFileName;
 
 #define FRAME_COUNT 16
 #define SAMPLE_COUNT FRAME_COUNT
@@ -62,9 +62,13 @@
 
 void cleanup (void)
 {
+	if (sTestFileName)
+	{
 #ifndef DEBUG
-	unlink(sTestFileName);
+		unlink(sTestFileName);
 #endif
+		free(sTestFileName);
+	}
 }
 
 void ensure (int condition, const char *message)
@@ -113,7 +117,7 @@
 	afInitFileFormat(setup, fileFormat);
 	afInitChannels(setup, AF_DEFAULT_TRACK, 1);
 
-	ensure(createTemporaryFile("writeulaw", sTestFileName),
+	ensure(createTemporaryFile("writeulaw", &sTestFileName),
 		"could not create temporary file");
 	file = afOpenFile(sTestFileName, "w", setup);
 	afFreeFileSetup(setup);
