Skip to content

Commit

Permalink
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more ro…
Browse files Browse the repository at this point in the history
…bust.
  • Loading branch information
icculus committed Nov 26, 2001
1 parent 31f5b08 commit 92e234e
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions SDL_sound.c
Expand Up @@ -568,12 +568,15 @@ void Sound_FreeSample(Sound_Sample *sample)
/* nuke it... */
if (internal->rw != NULL) /* this condition is a "just in case" thing. */
SDL_RWclose(internal->rw);
assert(internal->buffer != NULL);
if (internal->buffer != sample->buffer)

if ((internal->buffer != NULL) && (internal->buffer != sample->buffer))
free(internal->buffer);

free(internal);

if (sample->buffer != NULL)
free(sample->buffer);

free(sample);
} /* Sound_FreeSample */

Expand Down Expand Up @@ -635,6 +638,7 @@ Uint32 Sound_DecodeAll(Sound_Sample *sample)
{
Sound_SampleInternal *internal = NULL;
void *buf = NULL;
void *ptr;
Uint32 newBufSize = 0;

BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
Expand All @@ -644,29 +648,34 @@ Uint32 Sound_DecodeAll(Sound_Sample *sample)
while ( ((sample->flags & SOUND_SAMPLEFLAG_EOF) == 0) &&
((sample->flags & SOUND_SAMPLEFLAG_ERROR) == 0) )
{
void *ptr = realloc(buf, newBufSize + sample->buffer_size);
ptr = realloc(buf, newBufSize + sample->buffer_size);
if (ptr == NULL)
{
sample->flags |= SOUND_SAMPLEFLAG_ERROR;
Sound_SetError(ERR_OUT_OF_MEMORY);
} /* if */
else
{
Uint32 br = Sound_Decode(sample);
Uint32 br;
buf = ptr;
br = Sound_Decode(sample);
memcpy( ((char *) buf) + newBufSize, sample->buffer, br );
newBufSize += br;
} /* else */
} /* while */

if (internal->buffer != sample->buffer)
free(internal->buffer);

free(sample->buffer);
sample->buffer = internal->buffer = internal->sdlcvt.buf = buf;

internal->sdlcvt.buf = internal->buffer = sample->buffer = buf;
sample->buffer_size = newBufSize;
internal->buffer_size = newBufSize / internal->sdlcvt.len_mult;
internal->sdlcvt.len_mult = internal->buffer_size;
internal->sdlcvt.len = internal->buffer_size;

return(newBufSize);
} /* Sound_DecodeAll */


/* end of SDL_sound.c ... */

0 comments on commit 92e234e

Please sign in to comment.