diff mbox

[FFmpeg-devel,2/2] avcodec/arbc: Use ff_reget_buffer()

Message ID 20190829190459.5171-2-michael@niedermayer.cc
State New
Headers show

Commit Message

Michael Niedermayer Aug. 29, 2019, 7:04 p.m. UTC
Fixes: Timeout (20sec -> 0.5sec)
Fixes: 16404/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_ARBC_fuzzer-5660216355454976

Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/arbc.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

Comments

James Almer Aug. 29, 2019, 7:33 p.m. UTC | #1
On 8/29/2019 4:04 PM, Michael Niedermayer wrote:
> Fixes: Timeout (20sec -> 0.5sec)
> Fixes: 16404/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_ARBC_fuzzer-5660216355454976
> 
> Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavcodec/arbc.c | 21 +++++++--------------
>  1 file changed, 7 insertions(+), 14 deletions(-)
> 
> diff --git a/libavcodec/arbc.c b/libavcodec/arbc.c
> index 06970f140b..f0de50e243 100644
> --- a/libavcodec/arbc.c
> +++ b/libavcodec/arbc.c
> @@ -136,15 +136,9 @@ static int decode_frame(AVCodecContext *avctx, void *data,
>      if (7 * nb_segments > bytestream2_get_bytes_left(&s->gb))
>          return AVERROR_INVALIDDATA;
>  
> -    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
> +    if ((ret = ff_reget_buffer(avctx, s->prev_frame)) < 0)
>          return ret;
>  
> -    if (s->prev_frame->data[0]) {
> -        ret = av_frame_copy(frame, s->prev_frame);
> -        if (ret < 0)
> -            return ret;
> -    }
> -
>      for (int i = 0; i < nb_segments; i++) {
>          int resolution_flag;
>          int fill;
> @@ -161,19 +155,18 @@ static int decode_frame(AVCodecContext *avctx, void *data,
>          resolution_flag = bytestream2_get_byte(&s->gb);
>  
>          if (resolution_flag & 0x10)
> -            prev_pixels -= fill_tileX(avctx, 1024, 1024, fill, frame);
> +            prev_pixels -= fill_tileX(avctx, 1024, 1024, fill, s->prev_frame);
>          if (resolution_flag & 0x08)
> -            prev_pixels -= fill_tileX(avctx, 256, 256, fill, frame);
> +            prev_pixels -= fill_tileX(avctx, 256, 256, fill, s->prev_frame);
>          if (resolution_flag & 0x04)
> -            prev_pixels -= fill_tileX(avctx, 64, 64, fill, frame);
> +            prev_pixels -= fill_tileX(avctx, 64, 64, fill, s->prev_frame);
>          if (resolution_flag & 0x02)
> -            prev_pixels -= fill_tileX(avctx, 16, 16, fill, frame);
> +            prev_pixels -= fill_tileX(avctx, 16, 16, fill, s->prev_frame);
>          if (resolution_flag & 0x01)
> -            prev_pixels -= fill_tile4(avctx, fill, frame);
> +            prev_pixels -= fill_tile4(avctx, fill, s->prev_frame);
>      }
>  
> -    av_frame_unref(s->prev_frame);
> -    if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
> +    if ((ret = av_frame_ref(frame, s->prev_frame)) < 0)
>          return ret;
>  
>      frame->pict_type = prev_pixels <= 0 ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;

You could while at it revert 8b10f09fd53, then implement d70bbdc5fa for
this decoder as part of this patch.
James Almer Aug. 30, 2019, 2:15 p.m. UTC | #2
On 8/29/2019 4:33 PM, James Almer wrote:
> On 8/29/2019 4:04 PM, Michael Niedermayer wrote:
>> Fixes: Timeout (20sec -> 0.5sec)
>> Fixes: 16404/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_ARBC_fuzzer-5660216355454976
>>
>> Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
>> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
>> ---
>>  libavcodec/arbc.c | 21 +++++++--------------
>>  1 file changed, 7 insertions(+), 14 deletions(-)
>>
>> diff --git a/libavcodec/arbc.c b/libavcodec/arbc.c
>> index 06970f140b..f0de50e243 100644
>> --- a/libavcodec/arbc.c
>> +++ b/libavcodec/arbc.c
>> @@ -136,15 +136,9 @@ static int decode_frame(AVCodecContext *avctx, void *data,
>>      if (7 * nb_segments > bytestream2_get_bytes_left(&s->gb))
>>          return AVERROR_INVALIDDATA;
>>  
>> -    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
>> +    if ((ret = ff_reget_buffer(avctx, s->prev_frame)) < 0)
>>          return ret;
>>  
>> -    if (s->prev_frame->data[0]) {
>> -        ret = av_frame_copy(frame, s->prev_frame);
>> -        if (ret < 0)
>> -            return ret;
>> -    }
>> -
>>      for (int i = 0; i < nb_segments; i++) {
>>          int resolution_flag;
>>          int fill;
>> @@ -161,19 +155,18 @@ static int decode_frame(AVCodecContext *avctx, void *data,
>>          resolution_flag = bytestream2_get_byte(&s->gb);
>>  
>>          if (resolution_flag & 0x10)
>> -            prev_pixels -= fill_tileX(avctx, 1024, 1024, fill, frame);
>> +            prev_pixels -= fill_tileX(avctx, 1024, 1024, fill, s->prev_frame);
>>          if (resolution_flag & 0x08)
>> -            prev_pixels -= fill_tileX(avctx, 256, 256, fill, frame);
>> +            prev_pixels -= fill_tileX(avctx, 256, 256, fill, s->prev_frame);
>>          if (resolution_flag & 0x04)
>> -            prev_pixels -= fill_tileX(avctx, 64, 64, fill, frame);
>> +            prev_pixels -= fill_tileX(avctx, 64, 64, fill, s->prev_frame);
>>          if (resolution_flag & 0x02)
>> -            prev_pixels -= fill_tileX(avctx, 16, 16, fill, frame);
>> +            prev_pixels -= fill_tileX(avctx, 16, 16, fill, s->prev_frame);
>>          if (resolution_flag & 0x01)
>> -            prev_pixels -= fill_tile4(avctx, fill, frame);
>> +            prev_pixels -= fill_tile4(avctx, fill, s->prev_frame);
>>      }
>>  
>> -    av_frame_unref(s->prev_frame);
>> -    if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
>> +    if ((ret = av_frame_ref(frame, s->prev_frame)) < 0)
>>          return ret;
>>  
>>      frame->pict_type = prev_pixels <= 0 ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
> 
> You could while at it revert 8b10f09fd53, then implement d70bbdc5fa for
> this decoder as part of this patch.

Or maybe we should do what you suggested and simplify things by either
adding a new reget function, or changing the current one to take a new
argument to request a writable buffer.
The latter sounds better, IMO, and I can look into implementing it.
Michael Niedermayer Aug. 30, 2019, 6:23 p.m. UTC | #3
On Fri, Aug 30, 2019 at 11:15:21AM -0300, James Almer wrote:
> On 8/29/2019 4:33 PM, James Almer wrote:
> > On 8/29/2019 4:04 PM, Michael Niedermayer wrote:
> >> Fixes: Timeout (20sec -> 0.5sec)
> >> Fixes: 16404/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_ARBC_fuzzer-5660216355454976
> >>
> >> Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> >> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> >> ---
> >>  libavcodec/arbc.c | 21 +++++++--------------
> >>  1 file changed, 7 insertions(+), 14 deletions(-)
> >>
> >> diff --git a/libavcodec/arbc.c b/libavcodec/arbc.c
> >> index 06970f140b..f0de50e243 100644
> >> --- a/libavcodec/arbc.c
> >> +++ b/libavcodec/arbc.c
> >> @@ -136,15 +136,9 @@ static int decode_frame(AVCodecContext *avctx, void *data,
> >>      if (7 * nb_segments > bytestream2_get_bytes_left(&s->gb))
> >>          return AVERROR_INVALIDDATA;
> >>  
> >> -    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
> >> +    if ((ret = ff_reget_buffer(avctx, s->prev_frame)) < 0)
> >>          return ret;
> >>  
> >> -    if (s->prev_frame->data[0]) {
> >> -        ret = av_frame_copy(frame, s->prev_frame);
> >> -        if (ret < 0)
> >> -            return ret;
> >> -    }
> >> -
> >>      for (int i = 0; i < nb_segments; i++) {
> >>          int resolution_flag;
> >>          int fill;
> >> @@ -161,19 +155,18 @@ static int decode_frame(AVCodecContext *avctx, void *data,
> >>          resolution_flag = bytestream2_get_byte(&s->gb);
> >>  
> >>          if (resolution_flag & 0x10)
> >> -            prev_pixels -= fill_tileX(avctx, 1024, 1024, fill, frame);
> >> +            prev_pixels -= fill_tileX(avctx, 1024, 1024, fill, s->prev_frame);
> >>          if (resolution_flag & 0x08)
> >> -            prev_pixels -= fill_tileX(avctx, 256, 256, fill, frame);
> >> +            prev_pixels -= fill_tileX(avctx, 256, 256, fill, s->prev_frame);
> >>          if (resolution_flag & 0x04)
> >> -            prev_pixels -= fill_tileX(avctx, 64, 64, fill, frame);
> >> +            prev_pixels -= fill_tileX(avctx, 64, 64, fill, s->prev_frame);
> >>          if (resolution_flag & 0x02)
> >> -            prev_pixels -= fill_tileX(avctx, 16, 16, fill, frame);
> >> +            prev_pixels -= fill_tileX(avctx, 16, 16, fill, s->prev_frame);
> >>          if (resolution_flag & 0x01)
> >> -            prev_pixels -= fill_tile4(avctx, fill, frame);
> >> +            prev_pixels -= fill_tile4(avctx, fill, s->prev_frame);
> >>      }
> >>  
> >> -    av_frame_unref(s->prev_frame);
> >> -    if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
> >> +    if ((ret = av_frame_ref(frame, s->prev_frame)) < 0)
> >>          return ret;
> >>  
> >>      frame->pict_type = prev_pixels <= 0 ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
> > 
> > You could while at it revert 8b10f09fd53, then implement d70bbdc5fa for
> > this decoder as part of this patch.
> 
> Or maybe we should do what you suggested and simplify things by either
> adding a new reget function, or changing the current one to take a new
> argument to request a writable buffer.

> The latter sounds better, IMO, and I can look into implementing it.

sure, ok

thx

[...]
diff mbox

Patch

diff --git a/libavcodec/arbc.c b/libavcodec/arbc.c
index 06970f140b..f0de50e243 100644
--- a/libavcodec/arbc.c
+++ b/libavcodec/arbc.c
@@ -136,15 +136,9 @@  static int decode_frame(AVCodecContext *avctx, void *data,
     if (7 * nb_segments > bytestream2_get_bytes_left(&s->gb))
         return AVERROR_INVALIDDATA;
 
-    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
+    if ((ret = ff_reget_buffer(avctx, s->prev_frame)) < 0)
         return ret;
 
-    if (s->prev_frame->data[0]) {
-        ret = av_frame_copy(frame, s->prev_frame);
-        if (ret < 0)
-            return ret;
-    }
-
     for (int i = 0; i < nb_segments; i++) {
         int resolution_flag;
         int fill;
@@ -161,19 +155,18 @@  static int decode_frame(AVCodecContext *avctx, void *data,
         resolution_flag = bytestream2_get_byte(&s->gb);
 
         if (resolution_flag & 0x10)
-            prev_pixels -= fill_tileX(avctx, 1024, 1024, fill, frame);
+            prev_pixels -= fill_tileX(avctx, 1024, 1024, fill, s->prev_frame);
         if (resolution_flag & 0x08)
-            prev_pixels -= fill_tileX(avctx, 256, 256, fill, frame);
+            prev_pixels -= fill_tileX(avctx, 256, 256, fill, s->prev_frame);
         if (resolution_flag & 0x04)
-            prev_pixels -= fill_tileX(avctx, 64, 64, fill, frame);
+            prev_pixels -= fill_tileX(avctx, 64, 64, fill, s->prev_frame);
         if (resolution_flag & 0x02)
-            prev_pixels -= fill_tileX(avctx, 16, 16, fill, frame);
+            prev_pixels -= fill_tileX(avctx, 16, 16, fill, s->prev_frame);
         if (resolution_flag & 0x01)
-            prev_pixels -= fill_tile4(avctx, fill, frame);
+            prev_pixels -= fill_tile4(avctx, fill, s->prev_frame);
     }
 
-    av_frame_unref(s->prev_frame);
-    if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
+    if ((ret = av_frame_ref(frame, s->prev_frame)) < 0)
         return ret;
 
     frame->pict_type = prev_pixels <= 0 ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;