From 92deb3cc5d606ce01697f80ae234dc8019954f69 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 21 Aug 2014 11:41:57 +0200 Subject: [PATCH] Make assert for invalid xmalloc YAZ-783 --- src/xmalloc.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/xmalloc.c b/src/xmalloc.c index f3ac0b7..db7c1f2 100644 --- a/src/xmalloc.c +++ b/src/xmalloc.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -22,6 +23,9 @@ #define TRACE_XMALLOC 1 #endif +/* treat any size >1/4 of max value of size_t to be an error */ +#define MALLOC_SIZE_MAX ((size_t)(-1) / 4) + static int log_level = 0; static int log_level_initialized = 0; @@ -244,8 +248,9 @@ void xmalloc_trav_f(const char *s, const char *file, int line) xmalloc_trav_d(file, line); } -void xmalloc_fatal(void) +void xmalloc_fatal(size_t size) { + assert(size < MALLOC_SIZE_MAX); exit(1); } @@ -266,7 +271,7 @@ void *xrealloc_f(void *o, size_t size, const char *file, int line) { yaz_log(YLOG_FATAL|YLOG_ERRNO, "Out of memory, realloc (%ld bytes)", (long) size); - xmalloc_fatal(); + xmalloc_fatal(size); } return p; } @@ -289,7 +294,7 @@ void *xmalloc_f(size_t size, const char *file, int line) { yaz_log(YLOG_FATAL, "Out of memory - malloc (%ld bytes)", (long) size); - xmalloc_fatal(); + xmalloc_fatal(size); } return p; } @@ -311,7 +316,7 @@ void *xcalloc_f(size_t nmemb, size_t size, const char *file, int line) { yaz_log(YLOG_FATAL, "Out of memory - calloc (%ld, %ld)", (long) nmemb, (long) size); - xmalloc_fatal(); + xmalloc_fatal(size); } return p; } -- 1.7.10.4