[dovecot-cvs] dovecot/src/lib mempool-alloconly.c,1.35,1.35.2.1

cras at dovecot.org cras at dovecot.org
Wed May 10 13:46:46 EEST 2006


Update of /var/lib/cvs/dovecot/src/lib
In directory talvi:/tmp/cvs-serv23490

Modified Files:
      Tag: branch_1_0
	mempool-alloconly.c 
Log Message:
base_size variable isn't really needed in the pool when DEBUG isn't used.
With DEBUG enabled, check when clearing the pool that the allocated memory
is completely cleared, ie. that no-one wrote out of bounds to it.



Index: mempool-alloconly.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/mempool-alloconly.c,v
retrieving revision 1.35
retrieving revision 1.35.2.1
diff -u -d -r1.35 -r1.35.2.1
--- mempool-alloconly.c	14 Jan 2006 17:23:22 -0000	1.35
+++ mempool-alloconly.c	10 May 2006 10:46:44 -0000	1.35.2.1
@@ -18,10 +18,10 @@
 	struct pool pool;
 	int refcount;
 
-	size_t base_size;
 	struct pool_block *block;
 #ifdef DEBUG
 	const char *name;
+	size_t base_size;
 #endif
 };
 
@@ -39,6 +39,8 @@
 #define POOL_BLOCK_DATA(block) \
 	((char *) (block) + SIZEOF_POOLBLOCK)
 
+#define DEFAULT_BASE_SIZE MEM_ALIGN(sizeof(struct alloconly_pool))
+
 static const char *pool_alloconly_get_name(pool_t pool);
 static void pool_alloconly_ref(pool_t pool);
 static void pool_alloconly_unref(pool_t *pool);
@@ -69,6 +71,21 @@
 	FALSE
 };
 
+#ifdef DEBUG
+static void check_nuls(struct pool_block *block)
+{
+	const unsigned char *data = POOL_BLOCK_DATA(block);
+	size_t i;
+
+	for (i = block->size - block->left; i < block->size; i++) {
+		if (data[i] != '\0')
+			i_unreached();
+	}
+	if (block->prev != NULL)
+		check_nuls(block->prev, 0);
+}
+#endif
+
 pool_t pool_alloconly_create(const char *name __attr_unused__, size_t size)
 {
 	struct alloconly_pool apool, *new_apool;
@@ -92,11 +109,11 @@
 	*new_apool = apool;
 #ifdef DEBUG
 	new_apool->name = p_strdup(&new_apool->pool, name);
-#endif
 
 	/* set base_size so p_clear() doesn't trash alloconly_pool structure. */
 	new_apool->base_size = new_apool->block->size - new_apool->block->left;
 	new_apool->block->last_alloc_size = 0;
+#endif
 
 	return &new_apool->pool;
 }
@@ -270,7 +287,11 @@
 {
 	struct alloconly_pool *apool = (struct alloconly_pool *) pool;
 	struct pool_block *block;
-	size_t avail_size;
+	size_t base_size, avail_size;
+
+#ifdef DEBUG
+	check_nuls(apool->block);
+#endif
 
 	/* destroy all blocks but the oldest, which contains the
 	   struct alloconly_pool allocation. */
@@ -287,8 +308,13 @@
 	}
 
 	/* clear the first block */
-	avail_size = apool->block->size - apool->base_size;
-	memset(PTR_OFFSET(POOL_BLOCK_DATA(apool->block), apool->base_size), 0,
+#ifdef DEBUG
+	base_size = apool->base_size;
+#else
+	base_size = DEFAULT_BASE_SIZE;
+#endif
+	avail_size = apool->block->size - base_size;
+	memset(PTR_OFFSET(POOL_BLOCK_DATA(apool->block), base_size), 0,
 	       avail_size - apool->block->left);
 	apool->block->left = avail_size;
 	apool->block->last_alloc_size = 0;



More information about the dovecot-cvs mailing list