diff mbox series

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

Message ID 20211204213258.11971-2-michael@niedermayer.cc
State New
Headers show
Series [FFmpeg-devel,1/4] avformat/mov: Check for EOF in mov_read_glbl()
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Michael Niedermayer Dec. 4, 2021, 9:32 p.m. UTC
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@niedermayer.cc>
---
 libavcodec/avpacket.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Anton Khirnov Dec. 6, 2021, 10:04 a.m. UTC | #1
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@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.
Michael Niedermayer Dec. 6, 2021, 10:17 a.m. UTC | #2
On Mon, Dec 06, 2021 at 11:04:09AM +0100, Anton Khirnov wrote:
> 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@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

above comment added

thx

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

[...]
Michael Niedermayer Dec. 6, 2021, 1:19 p.m. UTC | #3
On Mon, Dec 06, 2021 at 11:17:17AM +0100, Michael Niedermayer wrote:
> On Mon, Dec 06, 2021 at 11:04:09AM +0100, Anton Khirnov wrote:
> > 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@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
> 
> above comment added

will apply

[...]
diff mbox series

Patch

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);
             if (ret < 0) {
                 pkt->data = old_data;
                 return ret;