Re: [PATCH v6 3/8] vfio: selftests: Introduce a sysfs lib

From: David Matlack

Date: Wed Mar 04 2026 - 17:54:01 EST


On 2026-03-03 07:38 PM, Raghavendra Rao Ananta wrote:

> + ret = readlink(path, vf_path, PATH_MAX);
> + VFIO_ASSERT_NE(ret, -1);
> + vf_path[ret] = '\0';

...

> + ret = readlink(path, dev_iommu_group_path, sizeof(dev_iommu_group_path));
> + VFIO_ASSERT_NE(ret, -1, "Failed to get the IOMMU group for device: %s\n", bdf);
> + dev_iommu_group_path[ret] = '\0';

...

> + ret = readlink(path, driver_path, PATH_MAX);
> + if (ret == -1) {
> + free(out_driver);
> +
> + if (errno == ENOENT)
> + return NULL;
> +
> + VFIO_FAIL("Failed to read %s\n", path);
> + }
> + driver_path[ret] = '\0';

These can all write off the end of the buffer if ret == PATH_MAX.

Also there is an inconsistency in these calls. 2 use hard-coded PATH_MAX
while the other uses sizeof().

Perhaps add a wrapper to handle all the details?

#define readlink_safe(path, buf) ({ \
int __ret = readlink(_path, _buf, sizeof(_buf) - 1); \
if (__ret != -1) \
_buf[__ret] = 0; \
__ret;
})