From 950336e0c34d9a022e6aff773558b94c0cc2a41e Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Fri, 7 Apr 2017 17:20:40 +0200 Subject: [PATCH] Fix potential buffer overflow. --- m-core.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/m-core.h b/m-core.h index 893dc898..0db9c51d 100644 --- a/m-core.h +++ b/m-core.h @@ -945,13 +945,13 @@ m_core_hash (const void *str, size_t wrdlen) # define M_MEMORY_ALLOC(type) ((type*)std::malloc (sizeof (type))) # define M_MEMORY_DEL(ptr) std::free(ptr) # define M_MEMORY_REALLOC(type, ptr, n) \ - ((type*) std::realloc ((ptr), (n)*sizeof (type))) + ((type*) M_UNLIKELY ((n) > SIZE_MAX / sizeof(type)) ? NULL : std::realloc ((ptr), (n)*sizeof (type))) # define M_MEMORY_FREE(ptr) std::free(ptr) #else # include # define M_MEMORY_ALLOC(type) malloc (sizeof (type)) # define M_MEMORY_DEL(ptr) free(ptr) -# define M_MEMORY_REALLOC(type, ptr, n) realloc ((ptr), (n)*sizeof (type)) +# define M_MEMORY_REALLOC(type, ptr, n) (M_UNLIKELY ((n) > SIZE_MAX / sizeof(type)) ? NULL : realloc ((ptr), (n)*sizeof (type))) # define M_MEMORY_FREE(ptr) free(ptr) #endif #endif