From 165792fb310080cd40bb891eabde97b3e6287001 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Mon, 18 Jan 2021 23:31:27 -0800 Subject: [PATCH] dup() can fail; check whether it does. If, for example, you run out of file descriptors, dup() can fail, and ws_dup() is a wrapper around it on UN*X. Don't just pass the result of ws_dup() to ws_fdopen(); instead, save its result, check against -1 and, if it's -1, give up, otherwise pass it to ws_fdopen(). This addresses Coverity CID 1471708. Also, if ws_fdopen() fails, close the descriptor we got from ws_dup(); this closes a possible FD leak. --- ui/qt/endpoint_dialog.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ui/qt/endpoint_dialog.cpp b/ui/qt/endpoint_dialog.cpp index 4578fd20d38..e0419bf9b35 100644 --- a/ui/qt/endpoint_dialog.cpp +++ b/ui/qt/endpoint_dialog.cpp @@ -229,10 +229,17 @@ QUrl EndpointDialog::createMap(bool json_only) return QUrl(); } // duplicate file descriptor as it is not allowed to perform a fclose before closing QFile - FILE* fp = ws_fdopen(ws_dup(fd), "wb"); + int duped_fd = ws_dup(fd); + if (duped_fd == -1) { + QMessageBox::warning(this, tr("Map file error"), tr("Unable to create temporary file")); + g_free(hosts); + return QUrl(); + } + FILE* fp = ws_fdopen(duped_fd, "wb"); if (fp == NULL) { QMessageBox::warning(this, tr("Map file error"), tr("Unable to create temporary file")); g_free(hosts); + ws_close(duped_fd); return QUrl(); }