Small bug in AudioSampleBuffer::setSize


#1

Hi Jules,

i think there is a small bug in AudioSampleBuffer::setSize.
When you increase the number of channels and keepExistingContent is true,
memcpy tries to copy undefined data from channel[i] to the new channels.

[code]void AudioSampleBuffer::setSize (const int newNumChannels,
const int newNumSamples,
const bool keepExistingContent,
const bool clearExtraSpace,
const bool avoidReallocating)
{
jassert (numChannels > 0 && numChannels <= maxNumAudioSampleBufferChannels);

if (newNumSamples != size || newNumChannels != numChannels)
{
    const int newTotalBytes = newNumChannels * newNumSamples * sizeof (float) + 32;

    if (keepExistingContent)
    {
        float* newData = (clearExtraSpace) ? (float*) juce_calloc (newTotalBytes)
                                           : (float*) juce_malloc (newTotalBytes);

        const int sizeToCopy = sizeof (float) * jmin (newNumSamples, size);

     for (int i = 0; i < newNumChannels; ++i) 
      {
				memcpy (newData + i * newNumSamples,   // <----!!!!!!!!
				     channels[i],
				     sizeToCopy);
			
		}

[/code]

So i added an if clause to prevent this


             for (int i = 0; i < newNumChannels; ++i) 
            {
				if ( i < numChannels )	 // NEW
				{
					memcpy (newData + i * newNumSamples,
					     channels[i],
					     sizeToCopy);
				};
			}

regards christian


#2

Hi there, and thanks! You’re quite right about that, thanks for spotting it!


#3