From 9fc7cc262f4bf6ae09af82efe59cbea7f7e330d7 Mon Sep 17 00:00:00 2001 From: Thanos Makatos Date: Wed, 4 Jan 2023 16:36:43 +0000 Subject: allow -1 file descriptor for ioregionfd (#727) Signed-off-by: Thanos Makatos Reviewed-by: John Levon --- lib/libvfio-user.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/libvfio-user.c b/lib/libvfio-user.c index 5a6269a..94524a2 100644 --- a/lib/libvfio-user.c +++ b/lib/libvfio-user.c @@ -483,7 +483,6 @@ vfu_create_ioeventfd(vfu_ctx_t *vfu_ctx, uint32_t region_idx, int fd, vfu_reg_info_t *vfu_reg; assert(vfu_ctx != NULL); - assert(fd >= 0); #ifndef SHADOW_IOEVENTFD if (shadow_fd != -1) { @@ -542,6 +541,15 @@ free_regions(vfu_ctx_t *vfu_ctx) * This function is used to add fd's to the fd return array and gives you back * the index of the fd that has been added. If the fd is already present it will * return the index to that duplicate fd to reduce the number of fd's sent. + * The fd must be a valid fd or -1, any other negative value is not permitted. + * + * out_fds: an array where the fd is stored + * nr_out_fds: pointer to memory that contains the size of the array + * fd_search: the fd to add + * + * returns: the array index where the fd is added to, can be the index of an + * existing fd if this is a duplicate fd. If the fd is -1 then the function + * returns -1. */ static int add_fd_index(int *out_fds, size_t *nr_out_fds, int fd_search) @@ -551,6 +559,11 @@ add_fd_index(int *out_fds, size_t *nr_out_fds, int fd_search) assert(out_fds != NULL); assert(nr_out_fds != NULL); + assert(fd_search >= -1); + if (fd_search == -1) { + return -1; + } + for (i = 0; i < *nr_out_fds; i++) { if (out_fds[i] == fd_search) { return i; -- cgit v1.1