diff mbox series

[FFmpeg-devel,1/2] fftools/ffmpeg_opt: Improve checks for truncation/alloc error

Message ID AM7PR03MB666052AAF01538FBD68B8EE98F6C9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Headers show
Series [FFmpeg-devel,1/2] fftools/ffmpeg_opt: Improve checks for truncation/alloc error
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/configureppc warning Failed to apply patch

Commit Message

Andreas Rheinhardt Dec. 5, 2021, 4:07 p.m. UTC
Do this by switching from the dynamic buffer API to the AVBPrint API;
the former has no defined way to check for errors.
This also avoids allocating an AVIOContext.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 fftools/ffmpeg_opt.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

Comments

Andreas Rheinhardt Dec. 6, 2021, 8:57 p.m. UTC | #1
Andreas Rheinhardt:
> Do this by switching from the dynamic buffer API to the AVBPrint API;
> the former has no defined way to check for errors.
> This also avoids allocating an AVIOContext.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  fftools/ffmpeg_opt.c | 22 ++++++++++------------
>  1 file changed, 10 insertions(+), 12 deletions(-)
> 
> diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
> index 6c2eb53290..78b5574a3d 100644
> --- a/fftools/ffmpeg_opt.c
> +++ b/fftools/ffmpeg_opt.c
> @@ -34,6 +34,7 @@
>  #include "libavutil/avassert.h"
>  #include "libavutil/avstring.h"
>  #include "libavutil/avutil.h"
> +#include "libavutil/bprint.h"
>  #include "libavutil/channel_layout.h"
>  #include "libavutil/intreadwrite.h"
>  #include "libavutil/fifo.h"
> @@ -1649,29 +1650,26 @@ static void parse_matrix_coeffs(uint16_t *dest, const char *str)
>  }
>  
>  /* read file contents into a string */
> -static uint8_t *read_file(const char *filename)
> +static char *read_file(const char *filename)
>  {
>      AVIOContext *pb      = NULL;
> -    AVIOContext *dyn_buf = NULL;
>      int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
> -    uint8_t buf[1024], *str;
> +    AVBPrint bprint;
> +    char *str;
>  
>      if (ret < 0) {
>          av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
>          return NULL;
>      }
>  
> -    ret = avio_open_dyn_buf(&dyn_buf);
> +    av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
> +    ret = avio_read_to_bprint(pb, &bprint, SIZE_MAX);
> +    avio_closep(&pb);
>      if (ret < 0) {
> -        avio_closep(&pb);
> +        av_bprint_finalize(&bprint, NULL);
>          return NULL;
>      }
> -    while ((ret = avio_read(pb, buf, sizeof(buf))) > 0)
> -        avio_write(dyn_buf, buf, ret);
> -    avio_w8(dyn_buf, 0);
> -    avio_closep(&pb);
> -
> -    ret = avio_close_dyn_buf(dyn_buf, &str);
> +    ret = av_bprint_finalize(&bprint, &str);
>      if (ret < 0)
>          return NULL;
>      return str;
> @@ -3279,7 +3277,7 @@ static int opt_filter_complex(void *optctx, const char *opt, const char *arg)
>  
>  static int opt_filter_complex_script(void *optctx, const char *opt, const char *arg)
>  {
> -    uint8_t *graph_desc = read_file(arg);
> +    char *graph_desc = read_file(arg);
>      if (!graph_desc)
>          return AVERROR(EINVAL);
>  
> 
Will apply this patchset tomorrow unless there are objections.

- Andreas
diff mbox series

Patch

diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 6c2eb53290..78b5574a3d 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -34,6 +34,7 @@ 
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/avutil.h"
+#include "libavutil/bprint.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/fifo.h"
@@ -1649,29 +1650,26 @@  static void parse_matrix_coeffs(uint16_t *dest, const char *str)
 }
 
 /* read file contents into a string */
-static uint8_t *read_file(const char *filename)
+static char *read_file(const char *filename)
 {
     AVIOContext *pb      = NULL;
-    AVIOContext *dyn_buf = NULL;
     int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
-    uint8_t buf[1024], *str;
+    AVBPrint bprint;
+    char *str;
 
     if (ret < 0) {
         av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
         return NULL;
     }
 
-    ret = avio_open_dyn_buf(&dyn_buf);
+    av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
+    ret = avio_read_to_bprint(pb, &bprint, SIZE_MAX);
+    avio_closep(&pb);
     if (ret < 0) {
-        avio_closep(&pb);
+        av_bprint_finalize(&bprint, NULL);
         return NULL;
     }
-    while ((ret = avio_read(pb, buf, sizeof(buf))) > 0)
-        avio_write(dyn_buf, buf, ret);
-    avio_w8(dyn_buf, 0);
-    avio_closep(&pb);
-
-    ret = avio_close_dyn_buf(dyn_buf, &str);
+    ret = av_bprint_finalize(&bprint, &str);
     if (ret < 0)
         return NULL;
     return str;
@@ -3279,7 +3277,7 @@  static int opt_filter_complex(void *optctx, const char *opt, const char *arg)
 
 static int opt_filter_complex_script(void *optctx, const char *opt, const char *arg)
 {
-    uint8_t *graph_desc = read_file(arg);
+    char *graph_desc = read_file(arg);
     if (!graph_desc)
         return AVERROR(EINVAL);