dovecot-2.2: DEBUG: Make sure errno isn't changed by vsnprintf()...

dovecot at dovecot.org dovecot at dovecot.org
Thu Apr 3 11:41:51 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/9b50caaa4467
changeset: 17185:9b50caaa4467
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Apr 03 14:41:25 2014 +0300
description:
DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
They shouldn't, so this should only be a sanity check when running with
devel-checks. Various parts in the code rely on errno not being changed by
*_strdup_printf() calls.

diffstat:

 src/lib/data-stack.c     |   4 ++++
 src/lib/mempool-system.c |  14 ++++++++++++++
 src/lib/strfuncs.c       |   7 +++++++
 3 files changed, 25 insertions(+), 0 deletions(-)

diffs (88 lines):

diff -r 2e275f4e51c3 -r 9b50caaa4467 src/lib/data-stack.c
--- a/src/lib/data-stack.c	Thu Apr 03 12:54:18 2014 +0300
+++ b/src/lib/data-stack.c	Thu Apr 03 14:41:25 2014 +0300
@@ -317,6 +317,7 @@
 	size_t alloc_size;
 #ifdef DEBUG
 	bool warn = FALSE;
+	int old_errno = errno;
 #endif
 
 	if (unlikely(size == 0 || size > SSIZE_T_MAX))
@@ -390,6 +391,9 @@
 	   had used t_buffer_get(). */
 	memset(PTR_OFFSET(ret, size), CLEAR_CHR,
 	       MEM_ALIGN(size + SENTRY_COUNT) - size);
+
+	/* we rely on errno not changing. it shouldn't. */
+	i_assert(errno == old_errno);
 #endif
         return ret;
 }
diff -r 2e275f4e51c3 -r 9b50caaa4467 src/lib/mempool-system.c
--- a/src/lib/mempool-system.c	Thu Apr 03 12:54:18 2014 +0300
+++ b/src/lib/mempool-system.c	Thu Apr 03 14:41:25 2014 +0300
@@ -73,6 +73,9 @@
 static void *pool_system_malloc(pool_t pool ATTR_UNUSED, size_t size)
 {
 	void *mem;
+#ifdef DEBUG
+	int old_errno = errno;
+#endif
 
 	if (unlikely(size == 0 || size > SSIZE_T_MAX))
 		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);
@@ -86,18 +89,29 @@
 		i_fatal_status(FATAL_OUTOFMEM, "pool_system_malloc(%"PRIuSIZE_T
 			       "): Out of memory", size);
 	}
+#ifdef DEBUG
+	/* we rely on errno not changing. it shouldn't. */
+	i_assert(errno == old_errno);
+#endif
 	return mem;
 }
 
 static void pool_system_free(pool_t pool ATTR_UNUSED,
 			     void *mem ATTR_UNUSED)
 {
+#ifdef DEBUG
+	int old_errno = errno;
+#endif
 #if !defined(USE_GC) && defined(HAVE_MALLOC_USABLE_SIZE) && defined(DEBUG)
 	safe_memset(mem, CLEAR_CHR, malloc_usable_size(mem));
 #endif
 #ifndef USE_GC
 	free(mem);
 #endif
+#ifdef DEBUG
+	/* we rely on errno not changing. it shouldn't. */
+	i_assert(errno == old_errno);
+#endif
 }
 
 static void *pool_system_realloc(pool_t pool ATTR_UNUSED, void *mem,
diff -r 2e275f4e51c3 -r 9b50caaa4467 src/lib/strfuncs.c
--- a/src/lib/strfuncs.c	Thu Apr 03 12:54:18 2014 +0300
+++ b/src/lib/strfuncs.c	Thu Apr 03 14:41:25 2014 +0300
@@ -106,6 +106,9 @@
 	char *tmp;
 	unsigned int init_size;
 	int ret;
+#ifdef DEBUG
+	int old_errno = errno;
+#endif
 
 	VA_COPY(args2, args);
 
@@ -127,6 +130,10 @@
 		ret = vsnprintf(tmp, *size_r, format, args2);
 		i_assert((unsigned int)ret == *size_r-1);
 	}
+#ifdef DEBUG
+	/* we rely on errno not changing. it shouldn't. */
+	i_assert(errno == old_errno);
+#endif
 	return tmp;
 }
 


More information about the dovecot-cvs mailing list