From edc5e59b7866a96d96b1e8fc1bab31aeb4681fca Mon Sep 17 00:00:00 2001 From: Alasdair Kergon Date: Wed, 24 Nov 2004 21:34:56 +0000 Subject: [PATCH] Trap large memory allocation requests. --- WHATS_NEW | 1 + lib/Makefile.in | 5 +---- lib/mm/dbg_malloc.c | 25 +++++++++++++++++-------- lib/mm/dbg_malloc.h | 10 +++++++--- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/WHATS_NEW b/WHATS_NEW index 0581ab45b..5dc464b37 100644 --- a/WHATS_NEW +++ b/WHATS_NEW @@ -1,5 +1,6 @@ Version 2.00.27 - 24th November 2004 ==================================== + Trap large memory allocation requests. Fix to partition table detection code. Improve filter debug mesgs. diff --git a/lib/Makefile.in b/lib/Makefile.in index 58041322d..4d57b736b 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -76,6 +76,7 @@ SOURCES =\ misc/crc.c \ misc/lvm-file.c \ misc/lvm-string.c \ + mm/dbg_malloc.c \ mm/memlock.c \ mm/pool.c \ regex/matcher.c \ @@ -121,10 +122,6 @@ ifeq ("@MIRRORS@", "internal") SOURCES += mirror/mirrored.c endif -ifeq ("@DEBUG@", "yes") - SOURCES += mm/dbg_malloc.c -endif - ifeq ("@DEVMAPPER@", "yes") SOURCES +=\ activate/dev_manager.c \ diff --git a/lib/mm/dbg_malloc.c b/lib/mm/dbg_malloc.c index 47cecf8c0..f0f8a6b57 100644 --- a/lib/mm/dbg_malloc.c +++ b/lib/mm/dbg_malloc.c @@ -19,6 +19,8 @@ #include +#ifdef DEBUG_MEM + struct memblock { struct memblock *prev, *next; /* All allocated blocks are linked */ size_t length; /* Size of the requested block */ @@ -47,7 +49,7 @@ void *malloc_aux(size_t s, const char *file, int line) if (s > 50000000) { log_error("Huge memory allocation (size %" PRIuPTR - ") rejected - bug?", s); + ") rejected - metadata corruption?", s); return 0; } @@ -166,7 +168,6 @@ void *realloc_aux(void *p, unsigned int s, const char *file, int line) return r; } -#ifdef DEBUG_MEM int dump_memory(void) { unsigned long tot = 0; @@ -213,10 +214,18 @@ void bounds_check(void) mb = mb->next; } } -#endif -/* - * Local variables: - * c-file-style: "linux" - * End: - */ +#else + +void *malloc_aux(size_t s, const char *file, int line) +{ + if (s > 50000000) { + log_error("Huge memory allocation (size %" PRIuPTR + ") rejected - metadata corruption?", s); + return 0; + } + + return malloc(s); +} + +#endif diff --git a/lib/mm/dbg_malloc.h b/lib/mm/dbg_malloc.h index 6d6303e36..4d19672f3 100644 --- a/lib/mm/dbg_malloc.h +++ b/lib/mm/dbg_malloc.h @@ -20,22 +20,26 @@ #include #include -#ifdef DEBUG_MEM void *malloc_aux(size_t s, const char *file, int line); +# define dbg_malloc(s) malloc_aux((s), __FILE__, __LINE__) + +#ifdef DEBUG_MEM + void free_aux(void *p); void *realloc_aux(void *p, unsigned int s, const char *file, int line); int dump_memory(void); void bounds_check(void); -# define dbg_malloc(s) malloc_aux((s), __FILE__, __LINE__) # define dbg_free(p) free_aux(p) # define dbg_realloc(p, s) realloc_aux(p, s, __FILE__, __LINE__) + #else -# define dbg_malloc(s) malloc(s) + # define dbg_free(p) free(p) # define dbg_realloc(p, s) realloc(p, s) # define dump_memory() # define bounds_check() + #endif static inline char *dbg_strdup(const char *str)