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>
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user