diff options
author | Diego 'Flameeyes' Pettenò <flameeyes@gmail.com> | 2007-04-14 17:12:05 +0200 |
---|---|---|
committer | Diego 'Flameeyes' Pettenò <flameeyes@gmail.com> | 2007-04-14 17:12:05 +0200 |
commit | b10d5cccee5d2945b1924a732a9b7d9d79d91b0b (patch) | |
tree | 51f0349cbb1081af758eee5455f094d0b7c171d5 | |
parent | 9c7b147a1878bd2c9290bb3377f41bc4a27ddccd (diff) | |
download | xine-lib-b10d5cccee5d2945b1924a732a9b7d9d79d91b0b.tar.gz xine-lib-b10d5cccee5d2945b1924a732a9b7d9d79d91b0b.tar.bz2 |
Add a xine_xcalloc function to wrap around calloc(), to improve security from now on.
-rw-r--r-- | src/xine-utils/utils.c | 21 | ||||
-rw-r--r-- | src/xine-utils/xineutils.h | 6 |
2 files changed, 27 insertions, 0 deletions
diff --git a/src/xine-utils/utils.c b/src/xine-utils/utils.c index fa0c11dbe..63c2e2f09 100644 --- a/src/xine-utils/utils.c +++ b/src/xine-utils/utils.c @@ -256,6 +256,27 @@ void *xine_xmalloc(size_t size) { return ptr; } +/** + * @brief Wrapper around calloc() function. + * @param nmemb Number of elements to allocate + * @param size Size of each element to allocate + * + * This is a simple wrapper around calloc(), the only thing + * it does more than calloc() is outputting an error if + * the calloc fails (returning NULL). + */ +void *xine_xcalloc(size_t nmemb, size_t size) { + void *ptr; + + if((ptr = calloc(nmemb, size)) == NULL) { + fprintf(stderr, "%s: calloc() failed: %s.\n", + __XINE_FUNCTION__, strerror(errno)); + return NULL; + } + + return ptr; +} + void *xine_xmalloc_aligned(size_t alignment, size_t size, void **base) { char *ptr; diff --git a/src/xine-utils/xineutils.h b/src/xine-utils/xineutils.h index 03c5f689a..20c3cb2dc 100644 --- a/src/xine-utils/xineutils.h +++ b/src/xine-utils/xineutils.h @@ -630,6 +630,12 @@ void *xine_xmalloc(size_t size) XINE_PROTECTED; void *xine_xmalloc(size_t size) __attribute__ ((__malloc__)) XINE_PROTECTED; #endif +#if !defined(__GNUC__) || __GNUC__ < 3 +void *xine_xcalloc(size_t nmemb, size_t size) XINE_PROTECTED; +#else +void *xine_xcalloc(size_t nmemb, size_t size) __attribute__ ((__malloc__)) XINE_PROTECTED; +#endif + /* * Same as above, but memory is aligned to 'alignement'. * **base is used to return pointer to un-aligned memory, use |