diff mbox series

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

Message ID AM7PR03MB6660D1CC5D7B331D50064D0D8FC89@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit 9634e6b0b065896bfc02cce4b1687a22531e2ecf
Headers show
Series [FFmpeg-devel,01/25] avformat/matroskadec: Fix heap-buffer overflow upon gigantic timestamps | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Aug. 27, 2021, 2:27 p.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/tiff_common.c | 76 +++++++++++-----------------------------
 1 file changed, 21 insertions(+), 55 deletions(-)
diff mbox series

Patch

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,