diff mbox

[FFmpeg-devel] avcodec/mov: Allocate skipped_bytes_pos with av_realloc.

Message ID 20170831180743.10311-1-wachsler@google.com
State New
Headers show

Commit Message

Mark Wachsler Aug. 31, 2017, 6:07 p.m. UTC
Memory reallocated with av_reallocp_array is supposed to be allocated
with av_realloc, not av_malloc.
---
 libavcodec/h2645_parse.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Michael Niedermayer Sept. 1, 2017, 10:45 a.m. UTC | #1
On Thu, Aug 31, 2017 at 02:07:43PM -0400, Mark Wachsler wrote:
> Memory reallocated with av_reallocp_array is supposed to be allocated
> with av_realloc, not av_malloc.

Our docs did briefly claim this but it was removed years ago in
21f70940ae106bfffa07e73057cdb4b5e81a767a

Also FFmpeg has been written with av_realloc*() and av_malloc*()
compatibility in mind both before and after 21f70940ae106bfffa07e73057cdb4b5e81a767a
So its not 2 but many more that likely would need to be changed

If there is a incompatibility then
Its easier to fix the incompatibility between av_malloc* and av_realloc*
also it is much more maintainable if they are compatibile.
Code that works on 99.9 % of the developers systems but breaks on the
remaininng 0.1% is really hard to maintain design.

That said iam not against this patch and i can apply it (without tabs)
if people want but this is far from fixing all these issues

DId you try to replace posix_memalign() by aligned_alloc() where its
available?
ISO C requires aligned_alloc() to be compatibile with realloc()
where one can debate the situation of posix_memalign() and realloc()

For reference
    7.22.3 Memory management functions
    ...
    7.22.3.1 The aligned_alloc function

Thus aligned_alloc() is a "Memory management function" in ISO C

In the list of undefined behaviors there is:
    - The pointer argument to the free or realloc function does not match a pointer
      earlier returned by a memory management function, or the space has been deallocated
      by a call to free or realloc (7.22.3.3, 7.22.3.5).

The undefined behavior is clearly limited to pointers not originating
from "Memory management functions", I can see no special case for
aligned_alloc()

In case your implemnatation of aligned_alloc() and realloc() are
incompatibile as well, please fix your implemnatation.

Thanks

[...]
diff mbox

Patch

diff --git a/libavcodec/h2645_parse.c b/libavcodec/h2645_parse.c
index b0d9ff66f0..1b2534b2e6 100644
--- a/libavcodec/h2645_parse.c
+++ b/libavcodec/h2645_parse.c
@@ -318,7 +318,8 @@  int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
 
             nal = &pkt->nals[pkt->nb_nals];
             nal->skipped_bytes_pos_size = 1024; // initial buffer size
-            nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
+            nal->skipped_bytes_pos = av_realloc(NULL, nal->skipped_bytes_pos_size *
+						sizeof(*nal->skipped_bytes_pos));
             if (!nal->skipped_bytes_pos)
                 return AVERROR(ENOMEM);