]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
authorTimo Sirainen <tss@iki.fi>
Thu, 3 Apr 2014 11:41:25 +0000 (14:41 +0300)
committerTimo Sirainen <tss@iki.fi>
Thu, 3 Apr 2014 11:41:25 +0000 (14:41 +0300)
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.

src/lib/data-stack.c
src/lib/mempool-system.c
src/lib/strfuncs.c

index e82e9383717920f77a95692143d9d2f66daa5742..204446602aee041c382ca6d4216ef540216ee9df 100644 (file)
@@ -317,6 +317,7 @@ static void *t_malloc_real(size_t size, bool permanent)
        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 @@ static void *t_malloc_real(size_t size, bool permanent)
           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;
 }
index 1b1ed568bb87e41280f5cbf6a3d844f066d690a5..5b0a40de4f850216a3f37b6275962c6c6e9555b5 100644 (file)
@@ -73,6 +73,9 @@ static void pool_system_unref(pool_t *pool ATTR_UNUSED)
 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 @@ static void *pool_system_malloc(pool_t pool ATTR_UNUSED, size_t size)
                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,
index a29576cd12372abf532e0467dfade3f8a45931d8..231d64e17fc19cfb0bfc5eb80282bddbc8cd385e 100644 (file)
@@ -106,6 +106,9 @@ char *t_noalloc_strdup_vprintf(const char *format, va_list args,
        char *tmp;
        unsigned int init_size;
        int ret;
+#ifdef DEBUG
+       int old_errno = errno;
+#endif
 
        VA_COPY(args2, args);
 
@@ -127,6 +130,10 @@ char *t_noalloc_strdup_vprintf(const char *format, va_list args,
                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;
 }