From 05a0f878264b9853d07f229ffff1bc21355157be Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Thu, 29 Apr 2010 20:03:06 +0300 Subject: [PATCH] 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 --- src/lib/buffer.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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; -- 2.47.3