summaryrefslogtreecommitdiff
path: root/libavutil/mem.c
Commit message (Collapse)AuthorAge
* avutil/mem: Handle fast allocations near UINT_MAX properlyAndreas Rheinhardt2022-07-06
| | | | | | | | | | | | | | | | | | | | | av_fast_realloc and av_fast_mallocz? store the size of the objects they allocate in an unsigned. Yet they overallocate and currently they can allocate more than UINT_MAX bytes in case a user has requested a size of about UINT_MAX * 16 / 17 or more if SIZE_MAX > UINT_MAX (and if the user increased max_alloc_size via av_max_alloc). In this case it is impossible to store the true size of the buffer via the unsigned*; future requests are likely to use the (re)allocation codepath even if the buffer is actually large enough because of the incorrect size. Fix this by ensuring that the actually allocated size always fits into an unsigned. (This entails erroring out in case the user requested more than UINT_MAX.) Reviewed-by: Tomas Härdin <tjoppen@acc.umu.se> Reviewed-by: Anton Khirnov <anton@khirnov.net> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* Remove unnecessary libavutil/(avutil|common|internal).h inclusionsAndreas Rheinhardt2022-02-24
| | | | | | | | | | Some of these were made possible by moving several common macros to libavutil/macros.h. While just at it, also improve the other headers a bit. Reviewed-by: Martin Storsjö <martin@martin.st> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avutil/mem: Deprecate av_mallocz_array()Andreas Rheinhardt2021-09-20
| | | | | | | | It does the same as av_calloc(), so one of them should be removed. Given that av_calloc() has the shorter name, it is retained. Reviewed-by: Paul B Mahol <onemda@gmail.com> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avutil/mem: Reinline av_size_mult() internallyAndreas Rheinhardt2021-08-12
| | | | | | | | | | | | | Since 580e168a945b65100ec2c25433f33bfacfe9f7be, av_size_mult() is no longer inlined; on systems where interposing is a thing, this also inhibits the compiler from inlining said function into the internal callers of said function, although inlining such a small function is typically beneficial: With GCC 10.3 on Ubuntu x64 and -O3 this decreases the size of av_realloc_array from 91B to 23B, from 129B to 81B for av_realloc_f and from 77B to 23B for each of av_malloc_array, av_mallocz_array and av_calloc. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* Remove unnecessary avassert.h inclusionsAndreas Rheinhardt2021-07-22
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* lavu/mem: un-inline av_size_mult()Anton Khirnov2021-06-11
| | | | There seems to be no compelling reason for it to be inline.
* avutil/mem: check for max_alloc_size in av_fast_malloc()James Almer2021-05-27
| | | | | | This puts av_fast_malloc*() in line with av_fast_realloc(). Signed-off-by: James Almer <jamrial@gmail.com>
* avutil/mem: make ff_fast_malloc() internal to mem.cJames Almer2021-05-27
| | | | Signed-off-by: James Almer <jamrial@gmail.com>
* avutil/mem: make max_alloc_size an atomic typeJames Almer2021-05-23
| | | | Signed-off-by: James Almer <jamrial@gmail.com>
* avutil/mem: Also poison new av_realloc-allocated blocksAndreas Rheinhardt2021-04-30
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avutil/mem: Use max_alloc_size as-isAndreas Rheinhardt2020-05-26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* lavu/mem: Make other alloc functions more similar to av_malloc().Carl Eugen Hoyos2020-04-12
| | | | | | | Do not limit the array allocation functions and av_calloc() to allocations of INT_MAX, instead depend on max_alloc_size like av_malloc(). Allows a workaround for ticket #7140.
* avutil/mem: Optimize fill32() by unrolling and using 64bitMichael Niedermayer2019-01-20
| | | | | Reviewed-by: Marton Balint <cus@passwd.hu> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
* lavu/mem: Allow allocations close to max_alloc_size with av_fast_realloc().Carl Eugen Hoyos2018-01-04
|
* lavu/mem: Do not realloc in av_fast_realloc() if size == min_size.Carl Eugen Hoyos2018-01-01
| | | | This can avoid OOM for min_size close to FFmpeg's arbitrary alloc limits.
* avutil: add alignment needed for AVX-512James Darnley2017-12-24
|
* Merge commit '04b0f0e371ff81b682274b574fb465ba4395c09f'James Almer2017-10-30
|\ | | | | | | | | | | | | * commit '04b0f0e371ff81b682274b574fb465ba4395c09f': mem: uninline av_malloc(z)_array() Merged-by: James Almer <jamrial@gmail.com>
| * mem: uninline av_malloc(z)_array()Anton Khirnov2017-04-26
| | | | | | | | | | | | Inlining public functions hardcodes their implementation into the ABI, so it should be avoided unless there is a very good reason for it. No such reason exists in this case.
* | Merge commit '4fb311c804098d78e5ce5f527f9a9c37536d3a08'Clément Bœsch2017-03-20
|\| | | | | | | | | | | | | | | | | | | * commit '4fb311c804098d78e5ce5f527f9a9c37536d3a08': Drop memalign hack Merged, as this may indeed be uneeded since 46e3936fb04d06550151e667357065e3f646da1a. Merged-by: Clément Bœsch <u@pkh.me>
| * Drop memalign hackDiego Biurrun2016-09-03
| | | | | | | | It no longer serves a useful purpose.
* | dynarray: Change AV_ to FF_ for AV_DYNARRAY_ADDTimothy Gu2016-07-31
| | | | | | | | | | The header is not installed and the macro isn't used outside libavutil, so it is obviously privat to libavutil. Make the name reflect that.
* | avutil/mem: fix memleakZhao Zhili2016-07-28
| | | | | | | | | | | | | | The original code assumes av_realloc() will free ptr if size is zero. The assumes is incorrect now. Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
* | avutil/mem: Add av_fast_mallocz()Michael Niedermayer2015-11-18
| | | | | | | | Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
* | Factor duplicated ff_fast_malloc() out into mem_internal.hMichael Niedermayer2015-07-13
| | | | | | | | | | | | | | | | internal.h is difficult to use due to circular dependancies mem.h is a public header ff_* is not public Alternative solutions probably are possible too Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
* | avutil/mem: Fix potential overflow in overallocation codeMichael Niedermayer2015-07-11
| | | | | | | | Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
* | libavutil/mem: use size_t for the length in av_strdup()Michael Niedermayer2015-05-10
| | | | | | | | | | | | the string length is not constrained to INT_MAX Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
* | avutil/mem: replace remaining void **/*** casts by memcpy()Michael Niedermayer2015-02-03
| | | | | | | | | | | | This is similar to 60392480181f24ebf3ab48d8ac3614705de90152 Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
* | Merge commit '60392480181f24ebf3ab48d8ac3614705de90152'Michael Niedermayer2015-02-01
|\| | | | | | | | | | | | | | | | | | | * commit '60392480181f24ebf3ab48d8ac3614705de90152': mem: fix pointer pointer aliasing violations Conflicts: libavutil/mem.c Merged-by: Michael Niedermayer <michaelni@gmx.at>
| * mem: fix pointer pointer aliasing violationsRémi Denis-Courmont2015-02-01
| | | | | | | | | | | | | | | | | | This uses explicit memory copying to read and write pointer to pointers of arbitrary object types. This works provided that the architecture uses the same representation for all pointer types (the previous code made that assumption already anyway). Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
* | Merge commit '8ddc32629a6d6be77256694c9e322dde134609f3'Michael Niedermayer2014-08-14
|\| | | | | | | | | | | | | | | | | | | | | | | * commit '8ddc32629a6d6be77256694c9e322dde134609f3': mem: add av_strndup() for duplicating substrings Conflicts: libavutil/mem.c libavutil/mem.h libavutil/version.h Merged-by: Michael Niedermayer <michaelni@gmx.at>
| * mem: add av_strndup() for duplicating substringsAnton Khirnov2014-08-13
| |
* | lavu/mem: add av_dynarray_add_nofree functionLukasz Marek2014-03-29
| | | | | | | | | | | | | | | | av_dynarray_add_nofree function have similar functionality as existing av_dynarray_add, but it doesn't deallocate memory on fails. Signed-off-by: Lukasz Marek <lukasz.m.luki@gmail.com>
* | avutil/mem: avoid using intptr_t to access void* in av_dynarray_add()Michael Niedermayer2014-03-22
| | | | | | | | Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
* | lavu/mem: reimplement the dynarray functions with the macro.Nicolas George2014-03-22
| | | | | | | | | | Signed-off-by: Nicolas George <george@nsup.org> Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
* | Merge commit 'cce3e0a49f0dd030262c28d9c53de0bd2fd909c4'Michael Niedermayer2013-11-14
|\| | | | | | | | | | | | | | | | | | | | | | | | | * commit 'cce3e0a49f0dd030262c28d9c53de0bd2fd909c4': Move av_fast_{m,re}alloc from lavc to lavu. Conflicts: libavcodec/avcodec.h libavcodec/utils.c libavutil/mem.c libavutil/version.h Merged-by: Michael Niedermayer <michaelni@gmx.at>
| * Move av_fast_{m,re}alloc from lavc to lavu.Anton Khirnov2013-11-14
| |
* | Merge remote-tracking branch 'qatar/master'Michael Niedermayer2013-10-17
|\| | | | | | | | | | | | | * qatar/master: mem: Make av_strdup allocate using av_realloc Merged-by: Michael Niedermayer <michaelni@gmx.at>
| * mem: Make av_strdup allocate using av_reallocMartin Storsjö2013-10-16
| | | | | | | | | | | | | | | | | | This makes sure that pointers from av_strdup are reallocable, which is used in av_dict_set if the AV_DICT_APPEND flag is set. Nothing should rely on pointers from av_strdup being aligned. Signed-off-by: Martin Storsjö <martin@martin.st>
* | Merge commit '67e285ceca1cb602a5ab87010b30d904527924fe'Michael Niedermayer2013-09-21
|\| | | | | | | | | | | | | * commit '67e285ceca1cb602a5ab87010b30d904527924fe': mem: Handle av_reallocp(..., 0) properly Merged-by: Michael Niedermayer <michaelni@gmx.at>
| * mem: Handle av_reallocp(..., 0) properlyMartin Storsjö2013-09-20
| | | | | | | | | | | | | | Previously this did a double free (and returned an error). Reported-by: Justin Ruggles Signed-off-by: Martin Storsjö <martin@martin.st>
* | Merge commit '3feb3d6ce4be0a09a9f8f13d613bed25b523b6e7'Michael Niedermayer2013-09-17
|\| | | | | | | | | | | | | | | | | | | | | | | | | * commit '3feb3d6ce4be0a09a9f8f13d613bed25b523b6e7': mem: Introduce av_reallocp Conflicts: doc/APIchanges libavutil/mem.c libavutil/mem.h libavutil/version.h Merged-by: Michael Niedermayer <michaelni@gmx.at>
| * mem: Introduce av_reallocpLuca Barbato2013-09-16
| |
* | Merge commit 'c3e6e8f06c42499bd020fd0b37f9542150e6067b'Michael Niedermayer2013-09-06
|\| | | | | | | | | | | | | | | | | | | * commit 'c3e6e8f06c42499bd020fd0b37f9542150e6067b': mem: Do not check unsigned values for negative size Conflicts: libavutil/mem.c Merged-by: Michael Niedermayer <michaelni@gmx.at>
| * mem: Do not check unsigned values for negative sizeDiego Biurrun2013-09-05
| |
* | Merge commit 'b634b36fcebfe16b837b6c4044f5d5cb99a75040'Michael Niedermayer2013-09-06
|\| | | | | | | | | | | | | | | | | | | | | * commit 'b634b36fcebfe16b837b6c4044f5d5cb99a75040': mem: Improve documentation wording and spelling Conflicts: libavutil/mem.c libavutil/mem.h Merged-by: Michael Niedermayer <michaelni@gmx.at>
| * mem: Improve documentation wording and spellingDiego Biurrun2013-09-05
| |
* | avutil/mem: Fix flipped conditionMichael Niedermayer2013-08-09
| | | | | | | | | | | | | | Fixes return code and later null pointer dereference Found-by: Laurent Butti <laurentb@gmail.com> Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
* | avutil/mem: simplify av_reallocp_array() by using av_realloc_f()Michael Niedermayer2013-06-05
| | | | | | | | Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
* | Merge commit '3b4feac1ec14f861bdd7f494f288f4d8dd7f449e'Michael Niedermayer2013-06-05
|\| | | | | | | | | | | | | | | | | | | | | | | | | | | | | * commit '3b4feac1ec14f861bdd7f494f288f4d8dd7f449e': movenc: Keep track of the allocated size for the cluster array mem: Add av_realloc_array and av_reallocp_array Conflicts: doc/APIchanges libavformat/movenc.c libavutil/mem.c libavutil/mem.h libavutil/version.h Merged-by: Michael Niedermayer <michaelni@gmx.at>
| * mem: Add av_realloc_array and av_reallocp_arrayMartin Storsjö2013-06-04
| | | | | | | | | | | | These help avoiding overflows and simplify error handling. Signed-off-by: Martin Storsjö <martin@martin.st>