[FFmpeg-cvslog] avcodec/tiff_common: Fix AVBPrint error checks

Andreas Rheinhardt git at videolan.org
Mon Aug 30 17:11:52 EEST 2021


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Wed Aug 25 09:31:16 2021 +0200| [9634e6b0b065896bfc02cce4b1687a22531e2ecf] | committer: Andreas Rheinhardt

avcodec/tiff_common: Fix AVBPrint error checks

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

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

 libavcodec/tiff_common.c | 76 +++++++++++++-----------------------------------
 1 file changed, 21 insertions(+), 55 deletions(-)

diff --git a/libavcodec/tiff_common.c b/libavcodec/tiff_common.c
index b3c6b96b57..2b872ea7e2 100644
--- a/libavcodec/tiff_common.c
+++ b/libavcodec/tiff_common.c
@@ -80,11 +80,26 @@ static const char *auto_sep(int count, const char *sep, int i, int columns)
         return columns < count ? "\n" : "";
 }
 
+static int bprint_to_avdict(AVBPrint *bp, const char *name,
+                            AVDictionary **metadata)
+{
+    char *ap;
+    int ret;
+
+    if (!av_bprint_is_complete(bp)) {
+        av_bprint_finalize(bp, NULL);
+        return AVERROR(ENOMEM);
+    }
+    if ((ret = av_bprint_finalize(bp, &ap)) < 0)
+        return ret;
+
+    return av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
+}
+
 int ff_tadd_rational_metadata(int count, const char *name, const char *sep,
                               GetByteContext *gb, int le, AVDictionary **metadata)
 {
     AVBPrint bp;
-    char *ap;
     int32_t nom, denom;
     int i;
 
@@ -101,16 +116,7 @@ int ff_tadd_rational_metadata(int count, const char *name, const char *sep,
         av_bprintf(&bp, "%s%7"PRId32":%-7"PRId32, auto_sep(count, sep, i, 4), nom, denom);
     }
 
-    if ((i = av_bprint_finalize(&bp, &ap))) {
-        return i;
-    }
-    if (!ap) {
-        return AVERROR(ENOMEM);
-    }
-
-    av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
-
-    return 0;
+    return bprint_to_avdict(&bp, name, metadata);
 }
 
 
@@ -118,7 +124,6 @@ int ff_tadd_long_metadata(int count, const char *name, const char *sep,
                           GetByteContext *gb, int le, AVDictionary **metadata)
 {
     AVBPrint bp;
-    char *ap;
     int i;
 
     if (count >= INT_MAX / sizeof(int32_t) || count <= 0)
@@ -132,16 +137,7 @@ int ff_tadd_long_metadata(int count, const char *name, const char *sep,
         av_bprintf(&bp, "%s%7i", auto_sep(count, sep, i, 8), ff_tget_long(gb, le));
     }
 
-    if ((i = av_bprint_finalize(&bp, &ap))) {
-        return i;
-    }
-    if (!ap) {
-        return AVERROR(ENOMEM);
-    }
-
-    av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
-
-    return 0;
+    return bprint_to_avdict(&bp, name, metadata);
 }
 
 
@@ -149,7 +145,6 @@ int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
                              GetByteContext *gb, int le, AVDictionary **metadata)
 {
     AVBPrint bp;
-    char *ap;
     int i;
 
     if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
@@ -163,16 +158,7 @@ int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
         av_bprintf(&bp, "%s%.15g", auto_sep(count, sep, i, 4), ff_tget_double(gb, le));
     }
 
-    if ((i = av_bprint_finalize(&bp, &ap))) {
-        return i;
-    }
-    if (!ap) {
-        return AVERROR(ENOMEM);
-    }
-
-    av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
-
-    return 0;
+    return bprint_to_avdict(&bp, name, metadata);
 }
 
 
@@ -180,7 +166,6 @@ int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
                             GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
 {
     AVBPrint bp;
-    char *ap;
     int i;
 
     if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
@@ -195,16 +180,7 @@ int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
         av_bprintf(&bp, "%s%5i", auto_sep(count, sep, i, 8), v);
     }
 
-    if ((i = av_bprint_finalize(&bp, &ap))) {
-        return i;
-    }
-    if (!ap) {
-        return AVERROR(ENOMEM);
-    }
-
-    av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
-
-    return 0;
+    return bprint_to_avdict(&bp, name, metadata);
 }
 
 
@@ -212,7 +188,6 @@ int ff_tadd_bytes_metadata(int count, const char *name, const char *sep,
                            GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
 {
     AVBPrint bp;
-    char *ap;
     int i;
 
     if (count >= INT_MAX / sizeof(int8_t) || count < 0)
@@ -227,16 +202,7 @@ int ff_tadd_bytes_metadata(int count, const char *name, const char *sep,
         av_bprintf(&bp, "%s%3i", auto_sep(count, sep, i, 16), v);
     }
 
-    if ((i = av_bprint_finalize(&bp, &ap))) {
-        return i;
-    }
-    if (!ap) {
-        return AVERROR(ENOMEM);
-    }
-
-    av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
-
-    return 0;
+    return bprint_to_avdict(&bp, name, metadata);
 }
 
 int ff_tadd_string_metadata(int count, const char *name,



More information about the ffmpeg-cvslog mailing list