History

avutil/mem: Use max_alloc_size as-is

The size of a single allocation performed by av_malloc() or av_realloc() is supposed to be bounded by max_alloc_size, which defaults to INT_MAX and can be set by the user; yet currently this is not completely honoured: The actual value used is max_alloc_size - 32. How this came to be can only be understood historically: a) 0ecca7a49f8e254c12a3a1de048d738bfbb614c6 disallowed allocations > INT_MAX. At that time the size parameter of av_malloc() was an unsigned and the commentary added ("lets disallow possible ambiguous cases") indicates that this was done as a precaution against calling the functions with negative int values. Genuinely limiting the size of allocations to INT_MAX doesn't seem to have been the intention given that at this time the memalign hack introduced in commit da9b170c6f06184a5114dc66afb8385cd0ffff83 (which when enabled increased the size of allocations slightly so that one can return a correctly aligned pointer that actually does not point to the beginning of the allocated buffer) was already present. b) Said memalign hack allocated 17 bytes more than actually desired, yet allocating 16 bytes more is actually enough and so this was changed in a9493601638b048c44751956d2360f215918800c; this commit also replaced INT_MAX by INT_MAX - 16 (and made the limit therefore a limit on the size of the allocated buffer), but kept the comment, although there is nothing ambiguous about allocating (INT_MAX - 16)..INT_MAX. c) 13dfce3d44f99a2d7df71aba8ae003d58db726f7 then increased 16 to 32 for AVX, 6b4c0be5586acad3bbafd7d2dd02a8328a5ab632 replaced INT_MAX by MAX_MALLOC_SIZE (which was of course defined to be INT_MAX) and 5a8e994287d8ef181c0a5eac537547d7059b4524 added max_alloc_size and made it user-selectable. d) 4fb311c804098d78e5ce5f527f9a9c37536d3a08 then dropped the memalign hack, yet it kept the -32 (probably because the comment about ambiguous cases was still present?), although it is no longer needed at all after this commit. Therefore this commit removes it and uses max_alloc_size directly.

Andreas Rheinhardt committed 6 years ago

Commit 731c7751 parent 1fd85281 file · +4 −6Browse files

Unified · Split

libavutil/mem.c+4−6
@@ -78,8 +78,7 @@ void *av_malloc(size_t size) · 77 unmodified lines
78{78{
79 void *ptr = NULL;79 void *ptr = NULL;
80 80
81 /* let's disallow possibly ambiguous cases */81 if (size > max_alloc_size)
82 if (size > (max_alloc_size - 32))
83 return NULL;82 return NULL;
84 83
85#if HAVE_POSIX_MEMALIGN84#if HAVE_POSIX_MEMALIGN
@@ -134,8 +133,7 @@ void *av_malloc(size_t size) · 48 unmodified lines
134 133
135void *av_realloc(void *ptr, size_t size)134void *av_realloc(void *ptr, size_t size)
136{135{
137 /* let's disallow possibly ambiguous cases */136 if (size > max_alloc_size)
138 if (size > (max_alloc_size - 32))
139 return NULL;137 return NULL;
140 138
141#if HAVE_ALIGNED_MALLOC139#if HAVE_ALIGNED_MALLOC
@@ -482,12 +480,12 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size) · 340 unmodified lines
482 if (min_size <= *size)480 if (min_size <= *size)
483 return ptr;481 return ptr;
484 482
485 if (min_size > max_alloc_size - 32) {483 if (min_size > max_alloc_size) {
486 *size = 0;484 *size = 0;
487 return NULL;485 return NULL;
488 }486 }
489 487
490 min_size = FFMIN(max_alloc_size - 32, FFMAX(min_size + min_size / 16 + 32, min_size));488 min_size = FFMIN(max_alloc_size, FFMAX(min_size + min_size / 16 + 32, min_size));
491 489
492 ptr = av_realloc(ptr, min_size);490 ptr = av_realloc(ptr, min_size);
493 /* we could set this to the unmodified min_size but this is safer491 /* we could set this to the unmodified min_size but this is safer

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t