# Small bug in AudioSampleBuffer::setSize

**URL:** <https://forum.juce.com/t/small-bug-in-audiosamplebuffer-setsize/1531>\
**Category:** General JUCE discussion\
**Created:** [March 8, 2007, 5:53pm UTC](https://forum.juce.com/t/small-bug-in-audiosamplebuffer-setsize/1531 "2007-03-08T17:53:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [March 8, 2007, 5:53pm UTC](https://forum.juce.com/t/small-bug-in-audiosamplebuffer-setsize/1531/1 "2007-03-08T17:53:05Z")

</div>

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

```auto

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

```

regards christian

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 9, 2007, 1:17pm UTC](https://forum.juce.com/t/small-bug-in-audiosamplebuffer-setsize/1531/2 "2007-03-09T13:17:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 12, 2017, 8:57pm UTC](https://forum.juce.com/t/small-bug-in-audiosamplebuffer-setsize/1531/3 "2017-05-12T20:57:24Z")

</div>

Closed on May 12, 2017, 8:57pm UTC


