From 65cc5e91cd1316346725828b4e994abac5d1cbe1 Mon Sep 17 00:00:00 2001 From: Devansh-567 Date: Mon, 22 Jun 2026 14:44:39 +0530 Subject: [PATCH] Safely encapsulate fdopen within DBus::FileDescriptor to prevent leaks --- client/proxy/commands.cc | 2 +- dbus/DBusPipe.cc | 8 ++++++++ dbus/DBusPipe.h | 2 ++ server/FilesTransferTask.cc | 2 +- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/client/proxy/commands.cc b/client/proxy/commands.cc index 4fbe863e..3279d337 100644 --- a/client/proxy/commands.cc +++ b/client/proxy/commands.cc @@ -554,7 +554,7 @@ command_get_xfiles_by_pipe(DBus::Connection& conn, const string& config_name, un vector files; - FILE* fin = fdopen(fd.get_fd(), "r"); + FILE* fin = fd.fdopen("r"); if (!fin) SN_THROW(IOErrorException("reading pipe failed, fdopen failed: " + stringerror(errno))); diff --git a/dbus/DBusPipe.cc b/dbus/DBusPipe.cc index 691975eb..74eb680f 100644 --- a/dbus/DBusPipe.cc +++ b/dbus/DBusPipe.cc @@ -65,6 +65,14 @@ namespace DBus return marshaller; } + FILE* + FileDescriptor::fdopen(const char* mode) + { + FILE* ret = ::fdopen(_fd, mode); + if (ret) + _fd = -1; + return ret; + } Pipe::Pipe() { diff --git a/dbus/DBusPipe.h b/dbus/DBusPipe.h index 13a15a61..63bacf6a 100644 --- a/dbus/DBusPipe.h +++ b/dbus/DBusPipe.h @@ -44,6 +44,8 @@ namespace DBus int get_fd() const { return _fd; } void set_fd(int fd) { _fd = fd; } + FILE* fdopen(const char* mode); + private: int _fd = -1; diff --git a/server/FilesTransferTask.cc b/server/FilesTransferTask.cc index b708e23d..a0f3d66b 100644 --- a/server/FilesTransferTask.cc +++ b/server/FilesTransferTask.cc @@ -33,7 +33,7 @@ FilesTransferTask::FilesTransferTask(const Files& files) void FilesTransferTask::run() { - FILE* fout = fdopen(get_write_end().get_fd(), "w"); + FILE* fout = get_write_end().fdopen("w"); if (!fout) SN_THROW(StreamException()); -- 2.47.3