diff mbox series

[FFmpeg-devel,1/2] avformat/oggdec: Factor buffer reallocation out

Message ID 20200501131657.3071-1-michael@niedermayer.cc
State Accepted
Commit 5d5b0bbcb7d1d71a19dcec9b71fe59a4deea2fcd
Headers show
Series [FFmpeg-devel,1/2] avformat/oggdec: Factor buffer reallocation out | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Michael Niedermayer May 1, 2020, 1:16 p.m. UTC
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavformat/oggdec.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

Comments

Lynne May 1, 2020, 4:47 p.m. UTC | #1
May 1, 2020, 14:16 by michael@niedermayer.cc:

> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavformat/oggdec.c | 25 +++++++++++++++++--------
>  1 file changed, 17 insertions(+), 8 deletions(-)
>
> diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c
> index c591bafddd..a9034ea61c 100644
> --- a/libavformat/oggdec.c
> +++ b/libavformat/oggdec.c
> @@ -297,6 +297,20 @@ static int data_packets_seen(const struct ogg *ogg)
>  return 0;
>  }
>  
> +static int buf_realloc(struct ogg_stream *os, int size)
> +{
> +    /* Even if invalid guarantee there's enough memory to read the page */
> +    if (os->bufsize - os->bufpos < size) {
> +        uint8_t *nb = av_realloc(os->buf, 2*os->bufsize + AV_INPUT_BUFFER_PADDING_SIZE);
> +        if (!nb)
> +            return AVERROR(ENOMEM);
> +        os->buf = nb;
> +        os->bufsize *= 2;
> +    }
>

Looking at the code, this is just av_fast_realloc.
You could change it to that, or you could just allocate MAX_PAGE_SIZE at the start and never reallocate. Its only 65k.
Up to you, feel free to commit whatever you choose.
Michael Niedermayer May 1, 2020, 7:07 p.m. UTC | #2
On Fri, May 01, 2020 at 06:47:54PM +0200, Lynne wrote:
> May 1, 2020, 14:16 by michael@niedermayer.cc:
> 
> > Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> > ---
> >  libavformat/oggdec.c | 25 +++++++++++++++++--------
> >  1 file changed, 17 insertions(+), 8 deletions(-)
> >
> > diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c
> > index c591bafddd..a9034ea61c 100644
> > --- a/libavformat/oggdec.c
> > +++ b/libavformat/oggdec.c
> > @@ -297,6 +297,20 @@ static int data_packets_seen(const struct ogg *ogg)
> >  return 0;
> >  }
> >  
> > +static int buf_realloc(struct ogg_stream *os, int size)
> > +{
> > +    /* Even if invalid guarantee there's enough memory to read the page */
> > +    if (os->bufsize - os->bufpos < size) {
> > +        uint8_t *nb = av_realloc(os->buf, 2*os->bufsize + AV_INPUT_BUFFER_PADDING_SIZE);
> > +        if (!nb)
> > +            return AVERROR(ENOMEM);
> > +        os->buf = nb;
> > +        os->bufsize *= 2;
> > +    }
> >
> 
> Looking at the code, this is just av_fast_realloc.
> You could change it to that, or you could just allocate MAX_PAGE_SIZE at the start and never reallocate. Its only 65k.
> Up to you, feel free to commit whatever you choose.

The code already allocates 65k at the start
so ill apply this so this bug/regression is fixed, we can then change it
dont want to leave this bug open until we found the perfect solution

thx

[...]
diff mbox series

Patch

diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c
index c591bafddd..a9034ea61c 100644
--- a/libavformat/oggdec.c
+++ b/libavformat/oggdec.c
@@ -297,6 +297,20 @@  static int data_packets_seen(const struct ogg *ogg)
     return 0;
 }
 
+static int buf_realloc(struct ogg_stream *os, int size)
+{
+    /* Even if invalid guarantee there's enough memory to read the page */
+    if (os->bufsize - os->bufpos < size) {
+        uint8_t *nb = av_realloc(os->buf, 2*os->bufsize + AV_INPUT_BUFFER_PADDING_SIZE);
+        if (!nb)
+            return AVERROR(ENOMEM);
+        os->buf = nb;
+        os->bufsize *= 2;
+    }
+
+    return 0;
+}
+
 static int ogg_read_page(AVFormatContext *s, int *sid, int probing)
 {
     AVIOContext *bc = s->pb;
@@ -378,14 +392,9 @@  static int ogg_read_page(AVFormatContext *s, int *sid, int probing)
     if (idx >= 0) {
         os = ogg->streams + idx;
 
-        /* Even if invalid guarantee there's enough memory to read the page */
-        if (os->bufsize - os->bufpos < size) {
-            uint8_t *nb = av_realloc(os->buf, 2*os->bufsize + AV_INPUT_BUFFER_PADDING_SIZE);
-            if (!nb)
-                return AVERROR(ENOMEM);
-            os->buf = nb;
-            os->bufsize *= 2;
-        }
+        ret = buf_realloc(os, size);
+        if (ret < 0)
+            return ret;
 
         readout_buf = os->buf + os->bufpos;
     } else {