From: Devansh-567 Date: Mon, 22 Jun 2026 09:14:39 +0000 (+0530) Subject: Safely encapsulate fdopen within DBus::FileDescriptor to prevent leaks X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=65cc5e91cd1316346725828b4e994abac5d1cbe1;p=thirdparty%2Fsnapper.git Safely encapsulate fdopen within DBus::FileDescriptor to prevent leaks --- 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());