[FFmpeg-cvslog] avformat/flac_picture: Simplify parsing title

Andreas Rheinhardt git at videolan.org
Sat Aug 28 17:52:41 EEST 2021


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Mon Aug 23 13:54:19 2021 +0200| [e304f5f7583de6fff0b36e043feacb6e02261a96] | committer: Andreas Rheinhardt

avformat/flac_picture: Simplify parsing title

Don't allocate the buffer for the title ourselves, leave it to
av_dict_set(). This simplifies freeing.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=e304f5f7583de6fff0b36e043feacb6e02261a96
---

 libavformat/flac_picture.c | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/libavformat/flac_picture.c b/libavformat/flac_picture.c
index e7660a7db1..3b69654ff6 100644
--- a/libavformat/flac_picture.c
+++ b/libavformat/flac_picture.c
@@ -35,7 +35,8 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t **bufp, int buf_size,
     const CodecMime *mime = ff_id3v2_mime_tags;
     enum AVCodecID id = AV_CODEC_ID_NONE;
     AVBufferRef *data = NULL;
-    uint8_t mimetype[64], *desc = NULL, *buf = *bufp;
+    uint8_t mimetype[64], *buf = *bufp;
+    const uint8_t *desc = NULL;
     GetByteContext g;
     AVStream *st;
     int width, height, ret = 0;
@@ -103,16 +104,13 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t **bufp, int buf_size,
         return 0;
     }
     if (len > 0) {
-        if (!(desc = av_malloc(len + 1))) {
-            return AVERROR(ENOMEM);
-        }
-
-        bytestream2_get_bufferu(&g, desc, len);
-        desc[len] = 0;
+        desc = g.buffer;
+        bytestream2_skipu(&g, len);
     }
 
     /* picture metadata */
     width  = bytestream2_get_be32u(&g);
+    ((uint8_t*)g.buffer)[-4] = '\0';   // NUL-terminate desc.
     height = bytestream2_get_be32u(&g);
     bytestream2_skipu(&g, 8);
 
@@ -124,8 +122,8 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t **bufp, int buf_size,
         if (len > MAX_TRUNC_PICTURE_SIZE || len >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
             av_log(s, AV_LOG_ERROR, "Attached picture metadata block too big %u\n", len);
             if (s->error_recognition & AV_EF_EXPLODE)
-                ret = AVERROR_INVALIDDATA;
-            goto fail;
+                return AVERROR_INVALIDDATA;
+            return 0;
         }
 
         // Workaround bug for flac muxers that writs truncated metadata picture block size if
@@ -139,21 +137,21 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t **bufp, int buf_size,
         } else {
             av_log(s, AV_LOG_ERROR, "Attached picture metadata block too short\n");
             if (s->error_recognition & AV_EF_EXPLODE)
-                ret = AVERROR_INVALIDDATA;
-            goto fail;
+                return AVERROR_INVALIDDATA;
+            return 0;
         }
     }
     if (trunclen == 0 && len >= buf_size - (buf_size >> 4)) {
         data = av_buffer_create(buf, buf_size + AV_INPUT_BUFFER_PADDING_SIZE,
                                 av_buffer_default_free, NULL, 0);
         if (!data)
-            RETURN_ERROR(AVERROR(ENOMEM));
+            return AVERROR(ENOMEM);
         *bufp = NULL;
         data->data += bytestream2_tell(&g);
         data->size  = len + AV_INPUT_BUFFER_PADDING_SIZE;
     } else {
     if (!(data = av_buffer_alloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) {
-        RETURN_ERROR(AVERROR(ENOMEM));
+        return AVERROR(ENOMEM);
     }
 
     if (trunclen == 0) {
@@ -181,13 +179,12 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t **bufp, int buf_size,
     st->codecpar->height     = height;
     av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
     if (desc)
-        av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
+        av_dict_set(&st->metadata, "title", desc, 0);
 
     return 0;
 
 fail:
     av_buffer_unref(&data);
-    av_freep(&desc);
 
     return ret;
 }



More information about the ffmpeg-cvslog mailing list