[FFmpeg-devel] [PATCH v2 2/3] avformat/file: dup file descriptor for pipe protocol

Zhao Zhili quinkblack at foxmail.com
Sun Dec 11 17:17:28 EET 2022


From: Zhao Zhili <zhilizhao at tencent.com>

This can fix read/write error when user close the file descriptor
earlier. Now user can close the file descriptor earlier to avoid
file descriptor leak. So it's safer in both way.
---
 libavformat/file.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/libavformat/file.c b/libavformat/file.c
index b8725c1f48..f7ebd52433 100644
--- a/libavformat/file.c
+++ b/libavformat/file.c
@@ -401,7 +401,9 @@ static int pipe_open(URLContext *h, const char *filename, int flags)
 #if HAVE_SETMODE
     setmode(fd, O_BINARY);
 #endif
-    c->fd = fd;
+    c->fd = dup(fd);
+    if (c->fd < 0)
+        return AVERROR(errno);
     h->is_streamed = 1;
     return 0;
 }
@@ -411,6 +413,7 @@ const URLProtocol ff_pipe_protocol = {
     .url_open            = pipe_open,
     .url_read            = file_read,
     .url_write           = file_write,
+    .url_close           = file_close,
     .url_get_file_handle = file_get_handle,
     .url_check           = file_check,
     .priv_data_size      = sizeof(FileContext),
-- 
2.25.1



More information about the ffmpeg-devel mailing list