[FFmpeg-devel] [PATCH 02/25] avcodec/tiff_common: Fix AVBPrint error checks

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Fri Aug 27 17:27:02 EEST 2021


Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 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,
-- 
2.30.2



More information about the ffmpeg-devel mailing list