dovecot-2.0: DEBUG: Try to catch stale pointer dereferences to b...

dovecot at dovecot.org dovecot at dovecot.org
Thu Apr 29 20:03:10 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/7037222941dc
changeset: 11223:7037222941dc
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Apr 29 20:03:06 2010 +0300
description:
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.

diffstat:

 src/lib/buffer.c |  16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)

diffs (26 lines):

diff -r 1b52e859933a -r 7037222941dc src/lib/buffer.c
--- a/src/lib/buffer.c	Thu Apr 29 19:59:51 2010 +0300
+++ b/src/lib/buffer.c	Thu Apr 29 20:03:06 2010 +0300
@@ -63,6 +63,22 @@
 		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;


More information about the dovecot-cvs mailing list