]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
DEBUG: Try to catch stale pointer dereferences to buffers after they've grown.
authorTimo Sirainen <tss@iki.fi>
Thu, 29 Apr 2010 17:03:06 +0000 (20:03 +0300)
committerTimo Sirainen <tss@iki.fi>
Thu, 29 Apr 2010 17:03:06 +0000 (20:03 +0300)
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

index 657b3824f9338dbe08add465d473521d1a5c1db9..f10a47e22888e76ad05361305ee940e8fbd621e1 100644 (file)
@@ -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;