From: Raghavendra Rao Ananta <rananta@google.com>
To: David Matlack <dmatlack@google.com>, Alex Williamson <alex@shazbot.org>
Cc: Vipin Sharma <vipinsh@google.com>,
Josh Hilke <jrhilke@google.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Raghavendra Rao Ananta <rananta@google.com>
Subject: [PATCH v6 2/8] vfio: selftests: Introduce snprintf_assert()
Date: Tue, 3 Mar 2026 19:38:16 +0000 [thread overview]
Message-ID: <20260303193822.2526335-3-rananta@google.com> (raw)
In-Reply-To: <20260303193822.2526335-1-rananta@google.com>
Introduce snprintf_assert() to protect the users of snprintf() to fail
if the requested operation was truncated due to buffer limits. VFIO
tests and libraries, including a new sysfs library that will be introduced
by an upcoming patch, rely quite heavily on snprintf()s to build PCI
sysfs paths. Having a protection against this will be helpful to prevent
false test failures.
Signed-off-by: Raghavendra Rao Ananta <rananta@google.com>
Reviewed-by: David Matlack <dmatlack@google.com>
---
.../vfio/lib/include/libvfio/assert.h | 5 +++++
.../selftests/vfio/lib/vfio_pci_device.c | 8 +++----
.../selftests/vfio/vfio_dma_mapping_test.c | 6 +++---
.../selftests/vfio/vfio_pci_device_test.c | 21 ++++++++++---------
4 files changed, 23 insertions(+), 17 deletions(-)
diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/assert.h b/tools/testing/selftests/vfio/lib/include/libvfio/assert.h
index f4ebd122d9b6f..77b68c7129a64 100644
--- a/tools/testing/selftests/vfio/lib/include/libvfio/assert.h
+++ b/tools/testing/selftests/vfio/lib/include/libvfio/assert.h
@@ -51,4 +51,9 @@
VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #_arg, __ret); \
} while (0)
+#define snprintf_assert(_s, _size, _fmt, ...) do { \
+ int __ret = snprintf(_s, _size, _fmt, ##__VA_ARGS__); \
+ VFIO_ASSERT_LT(__ret, _size); \
+} while (0)
+
#endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_ASSERT_H */
diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
index 4e5871f1ebc3b..1538d2b30ae59 100644
--- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
+++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
@@ -209,7 +209,7 @@ static unsigned int vfio_pci_get_group_from_dev(const char *bdf)
unsigned int group;
int ret;
- snprintf(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH, bdf);
+ snprintf_assert(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH, bdf);
ret = readlink(sysfs_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);
@@ -229,7 +229,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf
int group;
group = vfio_pci_get_group_from_dev(bdf);
- snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group);
+ snprintf_assert(group_path, sizeof(group_path), "/dev/vfio/%d", group);
device->group_fd = open(group_path, O_RDWR);
VFIO_ASSERT_GE(device->group_fd, 0, "open(%s) failed\n", group_path);
@@ -300,7 +300,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf)
cdev_path = calloc(PATH_MAX, 1);
VFIO_ASSERT_NOT_NULL(cdev_path);
- snprintf(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf);
+ snprintf_assert(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf);
dir = opendir(dir_path);
VFIO_ASSERT_NOT_NULL(dir, "Failed to open directory %s\n", dir_path);
@@ -310,7 +310,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf)
if (strncmp("vfio", entry->d_name, 4))
continue;
- snprintf(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name);
+ snprintf_assert(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name);
break;
}
diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
index abb170bdcef7e..7d0de8c79de13 100644
--- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
+++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
@@ -44,9 +44,9 @@ static int intel_iommu_mapping_get(const char *bdf, u64 iova,
FILE *file;
char *rest;
- snprintf(iommu_mapping_path, sizeof(iommu_mapping_path),
- "/sys/kernel/debug/iommu/intel/%s/domain_translation_struct",
- bdf);
+ snprintf_assert(iommu_mapping_path, sizeof(iommu_mapping_path),
+ "/sys/kernel/debug/iommu/intel/%s/domain_translation_struct",
+ bdf);
printf("Searching for IOVA 0x%lx in %s\n", iova, iommu_mapping_path);
diff --git a/tools/testing/selftests/vfio/vfio_pci_device_test.c b/tools/testing/selftests/vfio/vfio_pci_device_test.c
index 7c0fe8ce3a61f..93c11fd5e0818 100644
--- a/tools/testing/selftests/vfio/vfio_pci_device_test.c
+++ b/tools/testing/selftests/vfio/vfio_pci_device_test.c
@@ -39,16 +39,17 @@ FIXTURE_TEARDOWN(vfio_pci_device_test)
iommu_cleanup(self->iommu);
}
-#define read_pci_id_from_sysfs(_file) ({ \
- char __sysfs_path[PATH_MAX]; \
- char __buf[32]; \
- int __fd; \
- \
- snprintf(__sysfs_path, PATH_MAX, "/sys/bus/pci/devices/%s/%s", device_bdf, _file); \
- ASSERT_GT((__fd = open(__sysfs_path, O_RDONLY)), 0); \
- ASSERT_GT(read(__fd, __buf, ARRAY_SIZE(__buf)), 0); \
- ASSERT_EQ(0, close(__fd)); \
- (u16)strtoul(__buf, NULL, 0); \
+#define read_pci_id_from_sysfs(_file) ({ \
+ char __sysfs_path[PATH_MAX]; \
+ char __buf[32]; \
+ int __fd; \
+ \
+ snprintf_assert(__sysfs_path, PATH_MAX, "/sys/bus/pci/devices/%s/%s", \
+ device_bdf, _file); \
+ ASSERT_GT((__fd = open(__sysfs_path, O_RDONLY)), 0); \
+ ASSERT_GT(read(__fd, __buf, ARRAY_SIZE(__buf)), 0); \
+ ASSERT_EQ(0, close(__fd)); \
+ (u16)strtoul(__buf, NULL, 0); \
})
TEST_F(vfio_pci_device_test, config_space_read_write)
--
2.53.0.473.g4a7958ca14-goog
next prev parent reply other threads:[~2026-03-03 19:38 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-03 19:38 [PATCH v6 0/8] vfio: selftest: Add SR-IOV UAPI test Raghavendra Rao Ananta
2026-03-03 19:38 ` [PATCH v6 1/8] vfio: selftests: Add -Wall and -Werror to the Makefile Raghavendra Rao Ananta
2026-03-03 19:38 ` Raghavendra Rao Ananta [this message]
2026-03-03 19:38 ` [PATCH v6 3/8] vfio: selftests: Introduce a sysfs lib Raghavendra Rao Ananta
2026-03-04 22:53 ` David Matlack
2026-03-31 21:27 ` Raghavendra Rao Ananta
2026-03-09 22:06 ` Vipin Sharma
2026-03-31 21:34 ` Raghavendra Rao Ananta
2026-03-03 19:38 ` [PATCH v6 4/8] vfio: selftests: Extend container/iommufd setup for passing vf_token Raghavendra Rao Ananta
2026-03-03 19:38 ` [PATCH v6 5/8] vfio: selftests: Expose more vfio_pci_device functions Raghavendra Rao Ananta
2026-03-03 19:38 ` [PATCH v6 6/8] vfio: selftests: Add helper to set/override a vf_token Raghavendra Rao Ananta
2026-03-03 19:38 ` [PATCH v6 7/8] vfio: selftests: Add helpers to alloc/free vfio_pci_device Raghavendra Rao Ananta
2026-03-03 19:38 ` [PATCH v6 8/8] vfio: selftests: Add tests to validate SR-IOV UAPI Raghavendra Rao Ananta
2026-03-11 23:57 ` Vipin Sharma
2026-03-31 23:53 ` Raghavendra Rao Ananta
2026-04-01 0:10 ` David Matlack
2026-04-01 23:46 ` Raghavendra Rao Ananta
2026-04-01 23:50 ` David Matlack
2026-04-02 0:17 ` Raghavendra Rao Ananta
2026-04-02 16:02 ` David Matlack
2026-04-02 17:33 ` Raghavendra Rao Ananta
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260303193822.2526335-3-rananta@google.com \
--to=rananta@google.com \
--cc=alex@shazbot.org \
--cc=dmatlack@google.com \
--cc=jrhilke@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=vipinsh@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.