[FFmpeg-devel] [PATCH v2 1/2] avformat/rtspdec: fix mem leaks on init fail

Andriy Gelman andriy.gelman at gmail.com
Sun Oct 11 23:32:05 EEST 2020


From: Andriy Gelman <andriy.gelman at gmail.com>

Fixes #6334

Signed-off-by: Andriy Gelman <andriy.gelman at gmail.com>
---
 libavformat/rtspdec.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/libavformat/rtspdec.c b/libavformat/rtspdec.c
index ef084a8b2b..d6a903c026 100644
--- a/libavformat/rtspdec.c
+++ b/libavformat/rtspdec.c
@@ -690,10 +690,8 @@ static int rtsp_listen(AVFormatContext *s)
                 return 0; // We are ready for streaming
         } else if (methodcode == SETUP)
             ret = rtsp_read_setup(s, host, uri);
-        if (ret) {
-            ffurl_close(rt->rtsp_hd);
+        if (ret)
             return AVERROR_INVALIDDATA;
-        }
     }
 }
 
@@ -719,7 +717,7 @@ static int rtsp_read_header(AVFormatContext *s)
     if (rt->rtsp_flags & RTSP_FLAG_LISTEN) {
         ret = rtsp_listen(s);
         if (ret)
-            return ret;
+            goto fail;
     } else {
         ret = ff_rtsp_connect(s);
         if (ret)
@@ -727,22 +725,26 @@ static int rtsp_read_header(AVFormatContext *s)
 
         rt->real_setup_cache = !s->nb_streams ? NULL :
             av_mallocz_array(s->nb_streams, 2 * sizeof(*rt->real_setup_cache));
-        if (!rt->real_setup_cache && s->nb_streams)
-            return AVERROR(ENOMEM);
+        if (!rt->real_setup_cache && s->nb_streams) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
         rt->real_setup = rt->real_setup_cache + s->nb_streams;
 
         if (rt->initial_pause) {
             /* do not start immediately */
         } else {
             if ((ret = rtsp_read_play(s)) < 0) {
-                ff_rtsp_close_streams(s);
-                ff_rtsp_close_connections(s);
-                return ret;
+                goto fail;
             }
         }
     }
 
     return 0;
+
+fail:
+    rtsp_read_close(s);
+    return ret;
 }
 
 int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
-- 
2.28.0



More information about the ffmpeg-devel mailing list