From ac07438e6ab5233156c57c8b5ef6874b9c7e8700 Mon Sep 17 00:00:00 2001 From: Stefan Walter Date: Fri, 13 Feb 2009 18:32:47 +0000 Subject: [PATCH] Don't try and allocate 0 bytes when changing allocator on an empty buffer. svn path=/trunk/; revision=1565 --- egg/egg-buffer.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/egg/egg-buffer.c b/egg/egg-buffer.c index bbda991..eaff683 100644 --- a/egg/egg-buffer.c +++ b/egg/egg-buffer.c @@ -116,23 +116,25 @@ egg_buffer_uninit (EggBuffer *buffer) int egg_buffer_set_allocator (EggBuffer *buffer, EggBufferAllocator allocator) { - unsigned char *buf; + unsigned char *buf = NULL; if (!allocator) allocator = DEFAULT_ALLOCATOR; if (buffer->allocator == allocator) return 1; - /* Reallocate memory block using new allocator */ - buf = (allocator) (NULL, buffer->allocated_len); - if (!buf) - return 0; + if (buffer->allocated_len) { + /* Reallocate memory block using new allocator */ + buf = (allocator) (NULL, buffer->allocated_len); + if (buf == NULL) + return 0; - /* Copy stuff and free old memory */ - memcpy (buf, buffer->buf, buffer->allocated_len); + /* Copy stuff into new memory */ + memcpy (buf, buffer->buf, buffer->allocated_len); + } /* If old wasn't static, then free it */ - if (buffer->allocator) + if (buffer->allocator && buffer->buf) (buffer->allocator) (buffer->buf, 0); buffer->buf = buf; -- 2.7.4