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.


(cherry picked from commit 165792fb31)
This commit is contained in:
Guy Harris 2021-01-19 07:31:27 +00:00
parent f8d662d787
commit 22cc9f3f7f
1 changed files with 8 additions and 1 deletions

View File

@ -224,10 +224,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();
}