From c8953d1b4863e7484bf0afb42758644a16004cb9 Mon Sep 17 00:00:00 2001 From: Mansour Moufid Date: Fri, 14 Oct 2011 17:16:03 -0400 Subject: Add argument checks to some memory functions in `event.c'. Add a zero check to the function `event_mm_malloc_', i.e. simply return NULL if the sz argument is zero. On failure, set errno to ENOMEM and return NULL. Add a zero check to the function `event_mm_calloc_', i.e. simply return NULL if either argument is zero. Also add an unsigned integer multiplication check, and if an integer overflow would occur, set errno to ENOMEM and return NULL. On failure, set errno to ENOMEM and return NULL. Add a NULL check to the function `event_mm_strdup_', i.e. set errno to EINVAL and return NULL. Also add an unsigned integer addition check, and if an integer overflow would occur, set errno to ENOMEM and return NULL. If a memory allocation error occurs, again set errno to ENOMEM and return NULL. Add unit tests to `test/regress_util.c'. --- mm-internal.h | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'mm-internal.h') diff --git a/mm-internal.h b/mm-internal.h index 79855d6a..ea46037d 100644 --- a/mm-internal.h +++ b/mm-internal.h @@ -36,9 +36,35 @@ extern "C" { /* Internal use only: Memory allocation functions. We give them nice short * mm_names for our own use, but make sure that the symbols have longer names * so they don't conflict with other libraries (like, say, libmm). */ + +/** Allocate uninitialized memory. + * + * @return On success, return a pointer to sz newly allocated bytes. + * On failure, set errno to ENOMEM and return NULL. + * If the argument sz is 0, simply return NULL. + */ void *event_mm_malloc_(size_t sz); + +/** Allocate memory initialized to zero. + * + * @return On success, return a pointer to (count * size) newly allocated + * bytes, initialized to zero. + * On failure, or if the product would result in an integer overflow, + * set errno to ENOMEM and return NULL. + * If either arguments are 0, simply return NULL. + */ void *event_mm_calloc_(size_t count, size_t size); -char *event_mm_strdup_(const char *s); + +/** Duplicate a string. + * + * @return On success, return a pointer to a newly allocated duplicate + * of a string. + * Set errno to ENOMEM and return NULL if a memory allocation error + * occurs (or would occur) in the process. + * If the argument str is NULL, set errno to EINVAL and return NULL. + */ +char *event_mm_strdup_(const char *str); + void *event_mm_realloc_(void *p, size_t sz); void event_mm_free_(void *p); #define mm_malloc(sz) event_mm_malloc_(sz) -- cgit v1.2.1