diff options
author | Mansour Moufid <mansourmoufid@gmail.com> | 2011-10-14 17:16:03 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2011-10-19 23:21:57 -0400 |
commit | c8953d1b4863e7484bf0afb42758644a16004cb9 (patch) | |
tree | 60b1d461eec2211d822f0787e242281c49281491 /mm-internal.h | |
parent | 128c8d6c0f684b95a98358805eb91e1384dcb050 (diff) | |
download | libevent-c8953d1b4863e7484bf0afb42758644a16004cb9.tar.gz |
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'.
Diffstat (limited to 'mm-internal.h')
-rw-r--r-- | mm-internal.h | 28 |
1 files changed, 27 insertions, 1 deletions
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) |