Both function ease allocating large arrays implementing the overflow check inside it
Since pointless memsetting a potential large array is a waste of time the av_malloc_array is provided along as the av_calloc version. --- Hopefully this will make everybody happy. libavutil/mem.h | 36 +++++++++++++++++++++++++++++++++++- 1 files changed, 35 insertions(+), 1 deletions(-) diff --git a/libavutil/mem.h b/libavutil/mem.h index cd8490b..074e123 100644 --- a/libavutil/mem.h +++ b/libavutil/mem.h @@ -63,7 +63,7 @@ #endif #if AV_GCC_VERSION_AT_LEAST(4,3) - #define av_alloc_size(n) __attribute__((alloc_size(n))) + #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__))) #else #define av_alloc_size(n) #endif @@ -79,6 +79,22 @@ void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1); /** + * Helper function to allocate a block of size * nmemb bytes with + * using av_malloc() + * @param nmemb Number of elements + * @param size Size of the single element + * @return Pointer to the allocated block, NULL if the block cannot + * be allocated. + * @see av_malloc() + */ +av_alloc_size(1,2) static inline void *av_malloc_array(size_t nmemb, size_t size) +{ + if (size <= 0 || nmemb >= INT_MAX / size) + return NULL; + return av_malloc(nmemb * size); +} + +/** * Allocate or reallocate a block of memory. * If ptr is NULL and size > 0, allocate a new block. If * size is zero, free the memory block pointed to by ptr. @@ -113,6 +129,24 @@ void av_free(void *ptr); void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1); /** + * Helper function to allocate a block of size * nmemb bytes with + * using av_mallocz() + * @param nmemb Number of elements + * @param size Size of the single element + * @return Pointer to the allocated block, NULL if the block cannot + * be allocated. + * @see av_mallocz() + * @see av_malloc_array() + */ +av_alloc_size(1,2) static inline void *av_calloc(size_t nmemb, size_t size) +{ + if (size <= 0 || nmemb >= INT_MAX / size) + return NULL; + return av_mallocz(nmemb * size); +} + + +/** * Duplicate the string s. * @param s string to be duplicated * @return Pointer to a newly allocated string containing a -- 1.7.8.rc1 _______________________________________________ libav-devel mailing list libav-devel@libav.org https://lists.libav.org/mailman/listinfo/libav-devel