From: Adam Dickmeiss Date: Thu, 21 Aug 2014 09:41:57 +0000 (+0200) Subject: Make assert for invalid xmalloc YAZ-783 X-Git-Tag: v5.4.1~2 X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=commitdiff_plain;h=92deb3cc5d606ce01697f80ae234dc8019954f69 Make assert for invalid xmalloc YAZ-783 --- 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; }