[FFmpeg-devel] [PATCH 2/4] avcodec/avpacket: Perform fewer reallocations in repeated av_grow_packet()

Anton Khirnov anton at khirnov.net
Mon Dec 6 12:04:09 EET 2021


Quoting Michael Niedermayer (2021-12-04 22:32:56)
> Fixes: Timeout
> Fixes: 41446/clusterfuzz-testcase-minimized-ffmpeg_dem_SAMI_fuzzer-4667644540747776
> 
> Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> Signed-off-by: Michael Niedermayer <michael at niedermayer.cc>
> ---
>  libavcodec/avpacket.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
> index d8d8fef3b9e..b10498bfc1d 100644
> --- a/libavcodec/avpacket.c
> +++ b/libavcodec/avpacket.c
> @@ -142,7 +142,12 @@ int av_grow_packet(AVPacket *pkt, int grow_by)
>  
>          if (new_size + data_offset > pkt->buf->size ||
>              !av_buffer_is_writable(pkt->buf)) {
> -            int ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
> +            int ret;
> +
> +            if (new_size + data_offset < INT_MAX - new_size/16)
> +                new_size += new_size/16;
> +
> +            ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);

This needs a comment, e.g.
// allocate slightly more than requested to avoid excessive
// reallocations

Flow control in this function is overly complicated as it is.

-- 
Anton Khirnov


More information about the ffmpeg-devel mailing list