diff options
author | Martin Storsjö <martin@martin.st> | 2013-06-03 12:31:46 +0300 |
---|---|---|
committer | Martin Storsjö <martin@martin.st> | 2013-06-04 15:04:08 +0300 |
commit | fc962d4e7a7c3d799d9364d5427564c22ee3880c (patch) | |
tree | 4762a5e0b89bc406dce0c966c052393d7ae65859 /libavutil/mem.c | |
parent | 9683e37cd5c556c9974e78fba344ede4f33afd18 (diff) | |
download | ffmpeg-fc962d4e7a7c3d799d9364d5427564c22ee3880c.tar.gz |
mem: Add av_realloc_array and av_reallocp_array
These help avoiding overflows and simplify error handling.
Signed-off-by: Martin Storsjö <martin@martin.st>
Diffstat (limited to 'libavutil/mem.c')
-rw-r--r-- | libavutil/mem.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/libavutil/mem.c b/libavutil/mem.c index 391eb7db38..e901533cc7 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -136,6 +136,32 @@ void *av_realloc(void *ptr, size_t size) #endif } +void *av_realloc_array(void *ptr, size_t nmemb, size_t size) +{ + if (size <= 0 || nmemb >= INT_MAX / size) + return NULL; + return av_realloc(ptr, nmemb * size); +} + +int av_reallocp_array(void *ptr, size_t nmemb, size_t size) +{ + void **ptrptr = ptr; + void *ret; + if (size <= 0 || nmemb >= INT_MAX / size) + return AVERROR(ENOMEM); + if (nmemb <= 0) { + av_freep(ptr); + return 0; + } + ret = av_realloc(*ptrptr, nmemb * size); + if (!ret) { + av_freep(ptr); + return AVERROR(ENOMEM); + } + *ptrptr = ret; + return 0; +} + void av_free(void *ptr) { #if CONFIG_MEMALIGN_HACK |