io: avoid potential double close of qio_channel_file's fd

Make qio_channel_file_close() set fd to -1 even if qemu_close() returns
an error so that qio_channel_file_finalize() doesn't call close again.

On Linux, close() always closes the fd, even when it returns an error.
Because the fd can be reused after it is closed, the close must not
be retried.

Signed-off-by: Filip Hejsek <filip.hejsek@gmail.com>
Reviewed-by: Michael Tokarev <mjt@tls.msk.ru>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
This commit is contained in:
Filip Hejsek authored and Michael Tokarev committed 2026-11-12 13:30:13 +03:00
1 parent 29760efaf9
commit e5f54bcd1e
1 file changed
+15 -3
+15 -3
View File
@@ -261,12 +261,24 @@ static int qio_channel_file_close(QIOChannel *ioc,
{
QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
if (qemu_close(fioc->fd) < 0) {
int ret = qemu_close(fioc->fd);
/*
* Set fioc->fd to -1, to prevent qio_channel_file_finalize() from
* closing it again.
*
* POSIX.1-2024 specifies that the fd is closed even when close()
* returns an error, except when the error is EINTR. The systems that
* QEMU supports however violate the standard and always close the fd,
* even when returning EINTR. If the fd was actually closed, we must not
* try to close it a second time as we could accidentally close someone
* else's fd.
*/
fioc->fd = -1;
if (ret < 0) {
error_setg_errno(errp, errno,
"Unable to close file");
"Got an error while closing file");
return -1;
}
fioc->fd = -1;
return 0;
}