From: Timo Sirainen Date: Thu, 29 Apr 2010 17:03:06 +0000 (+0300) Subject: DEBUG: Try to catch stale pointer dereferences to buffers after they've grown. X-Git-Tag: 2.0.beta5~51 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=05a0f878264b9853d07f229ffff1bc21355157be;p=thirdparty%2Fdovecot%2Fcore.git DEBUG: Try to catch stale pointer dereferences to buffers after they've grown. In normal use some such bugs may not be noticed easily, because the buffer's memory allocation size is large enough that when adding another element the pointer doesn't change. --HG-- branch : HEAD --- diff --git a/src/lib/buffer.c b/src/lib/buffer.c index 657b3824f9..f10a47e228 100644 --- a/src/lib/buffer.c +++ b/src/lib/buffer.c @@ -63,6 +63,22 @@ buffer_check_limits(struct real_buffer *buf, size_t pos, size_t data_size) buffer_alloc(buf, pool_get_exp_grown_size(buf->pool, buf->alloc, new_size)); } +#ifdef DEBUG + else if (new_size > buf->used && buf->alloced && + !buf->pool->alloconly_pool && !buf->pool->datastack_pool) { + void *new_buf; + + /* buffer's size increased: move the buffer's memory elsewhere. + this should help catch bugs where old pointers are tried to + be used to access the buffer's memory */ + new_buf = p_malloc(buf->pool, buf->alloc); + memcpy(new_buf, buf->w_buffer, buf->alloc); + p_free(buf->pool, buf->w_buffer); + + buf->w_buffer = new_buf; + buf->r_buffer = new_buf; + } +#endif if (new_size > buf->used) buf->used = new_size;