diff mbox series

[FFmpeg-devel,5/5] avformat/img2enc: do not ignore IO errors

Message ID 20211130004950.30697-5-cus@passwd.hu
State Accepted
Commit 466441a0d27daf03c1bf023327da6cc4c610bed3
Headers show
Series [FFmpeg-devel,1/5] avformat/file: use proper return value in file_close | 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

Marton Balint Nov. 30, 2021, 12:49 a.m. UTC
Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/img2enc.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
index 62202de9f4..ce23f38efb 100644
--- a/libavformat/img2enc.c
+++ b/libavformat/img2enc.c
@@ -124,6 +124,13 @@  static int write_packet_pipe(AVFormatContext *s, AVPacket *pkt)
     return 0;
 }
 
+static int write_and_close(AVFormatContext *s, AVIOContext **pb, const unsigned char *buf, int size)
+{
+    avio_write(*pb, buf, size);
+    avio_flush(*pb);
+    return ff_format_io_close(s, pb);
+}
+
 static int write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     VideoMuxData *img = s->priv_data;
@@ -190,24 +197,22 @@  static int write_packet(AVFormatContext *s, AVPacket *pkt)
             ysize *= 2;
             usize *= 2;
         }
-        avio_write(pb[0], pkt->data                , ysize);
-        avio_write(pb[1], pkt->data + ysize        , usize);
-        avio_write(pb[2], pkt->data + ysize + usize, usize);
-        ff_format_io_close(s, &pb[1]);
-        ff_format_io_close(s, &pb[2]);
-        if (desc->nb_components > 3) {
-            avio_write(pb[3], pkt->data + ysize + 2*usize, ysize);
-            ff_format_io_close(s, &pb[3]);
-        }
+        if ((ret = write_and_close(s, &pb[0], pkt->data                , ysize)) < 0 ||
+            (ret = write_and_close(s, &pb[1], pkt->data + ysize        , usize)) < 0 ||
+            (ret = write_and_close(s, &pb[2], pkt->data + ysize + usize, usize)) < 0)
+            goto fail;
+        if (desc->nb_components > 3)
+            ret = write_and_close(s, &pb[3], pkt->data + ysize + 2*usize, ysize);
     } else if (img->muxer) {
-        ret = write_muxed_file(s, pb[0], pkt);
-        if (ret < 0)
+        if ((ret = write_muxed_file(s, pb[0], pkt)) < 0)
             goto fail;
+        ret = ff_format_io_close(s, &pb[0]);
     } else {
-        avio_write(pb[0], pkt->data, pkt->size);
+        ret = write_and_close(s, &pb[0], pkt->data, pkt->size);
     }
-    avio_flush(pb[0]);
-    ff_format_io_close(s, &pb[0]);
+    if (ret < 0)
+        goto fail;
+
     for (i = 0; i < nb_renames; i++) {
         int ret = ff_rename(img->tmp[i], img->target[i], s);
         if (ret < 0)