Re: [PATCH] tools: iio: Keep the buffer index separate from the acquired fd

From: Arnav Kapoor

Date: Fri Nov 20 2026 - 16:03:53 EST


On Fri, Nov 20, 2026 at 02:19:03PM +0800, Zhu Jun wrote:
> IIO_BUFFER_GET_FD_IOCTL takes the buffer index as input and replaces
> it with a file descriptor on success. On failure, cleanup can mistake
> the unchanged index for an open fd and close a different file. Only
> assign the returned descriptor to buf_fd after successful acquisition.

Checked against iio_device_buffer_getfd(): on every error path it
returns before copy_to_user(), so the argument keeps the index, and the
error: label then closes it because buf_fd >= 0. With the default
buffer index 0 that closes stdin, with -b 1 stdout. The patch fixes
that, buffer_idx is already rejected when negative, and it applies to
iio testing, builds without warnings and is checkpatch --strict clean.

Reviewed-by: Arnav Kapoor <kapoorarnav43@xxxxxxxxx>