Skip to contentSkip to conversation

selftests/vfio: Fix VLA initialisation in vfio_pci_irq_set()

PATCH In review [email protected] 3 participants 3 messages Mar 16

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/vfio: Fix VLA initialisation in vfio_pci_irq_set()

From: Manish Honap <[email protected]>

C does not permit an initialiser expression on a variable-length array (C99 Section 6.7.9 constraint: "The type of the entity to be initialized shall not be a variable length array type").

vfio_pci_irq_set() declared:

      u8 buf[sizeof(struct vfio_irq_set) + sizeof(int) * count] = {};

where count is a runtime function parameter, making buf a VLA.

GCC rejects this with (tried with GCC-9.4.0):

error: variable-sized object may not be initialized

Fix by removing the = {} initialiser and inserting an explicit memset() immediately after the declaration. memset() on a VLA is perfectly legal and achieves the same zero-initialisation on all conforming C implementations.

This fix is self-contained: it touches only the existing vfio selftest helper library and carries no dependency on any other patch. It was originally included as PATCH 20/20 in the CXL Type-2 VFIO passthrough RFC series [1] but belongs on the vfio list independently, as noted by Dave Jiang.

[1] https://lore.kernel.org/all/[email protected]/

Fixes 19faf6fd969c ("vfio: selftests: Add a helper library for VFIO selftests")
---
 tools/testing/selftests/vfio/lib/vfio_pci_device.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/lib/vfio_pci_device.c +3 -1
index fac4c0ecadef..3258e814f450 100644
@@ -26,8 +26,10 @@
 static void vfio_pci_irq_set(struct vfio_pci_device *device,
 			     u32 index, u32 vector, u32 count, int *fds)
 {
-	u8 buf[sizeof(struct vfio_irq_set) + sizeof(int) * count] = {};
+	u8 buf[sizeof(struct vfio_irq_set) + sizeof(int) * count];
 	struct vfio_irq_set *irq = (void *)&buf;
+
+	memset(buf, 0, sizeof(buf));
 	int *irq_fds = (void *)&irq->data;
 
 	irq->argsz = sizeof(buf);

--
2.25.1

Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · On 3/16/26 11:20 AM, [email protected] wrote:

From: Manish Honap <[email protected]>

C does not permit an initialiser expression on a variable-length array (C99 Section 6.7.9 constraint: "The type of the entity to be initialized shall not be a variable length array type").

vfio_pci_irq_set() declared:

      u8 buf[sizeof(struct vfio_irq_set) + sizeof(int) * count] = {};

where count is a runtime function parameter, making buf a VLA.

GCC rejects this with (tried with GCC-9.4.0):

error: variable-sized object may not be initialized

Fix by removing the = {} initialiser and inserting an explicit memset() immediately after the declaration. memset() on a VLA is perfectly legal and achieves the same zero-initialisation on all conforming C implementations.

This fix is self-contained: it touches only the existing vfio selftest helper library and carries no dependency on any other patch. It was originally included as PATCH 20/20 in the CXL Type-2 VFIO passthrough RFC series [1] but belongs on the vfio list independently, as noted by Dave Jiang.

Quoted 5 lines

[1] https://lore.kernel.org/all/[email protected]/

Fixes 19faf6fd969c ("vfio: selftests: Add a helper library for VFIO selftests")

You can drop this tag. I didn't suggest the changes. It's your patch. :)

Quoted 1 line
Quoted 24 lines
---
 tools/testing/selftests/vfio/lib/vfio_pci_device.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/lib/vfio_pci_device.c +3 -2
index fac4c0ecadef..3258e814f450 100644
@@ -26,8 +26,10 @@
 static void vfio_pci_irq_set(struct vfio_pci_device *device,
 			     u32 index, u32 vector, u32 count, int *fds)
 {
-	u8 buf[sizeof(struct vfio_irq_set) + sizeof(int) * count] = {};
+	u8 buf[sizeof(struct vfio_irq_set) + sizeof(int) * count];
 	struct vfio_irq_set *irq = (void *)&buf;
+
+	memset(buf, 0, sizeof(buf));
 	int *irq_fds = (void *)&irq->data;
 
 	irq->argsz = sizeof(buf);
--

2.25.1

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to [email protected]
Quoted 2 lines · On Mon, Mar 16, 2026 at 11:21 AM <[email protected]> wrote:

From: Manish Honap <[email protected]>

Please use "vfio: selftests: " instead of "selftests/vfio:" to match all the other changes to tools/testing/selftests/vfio.

  $ git log --oneline tools/testing/selftests/vfio
Quoted 19 lines

C does not permit an initialiser expression on a variable-length array (C99 Section 6.7.9 constraint: "The type of the entity to be initialized shall not be a variable length array type").

vfio_pci_irq_set() declared:

      u8 buf[sizeof(struct vfio_irq_set) + sizeof(int) * count] = {};

where count is a runtime function parameter, making buf a VLA.

GCC rejects this with (tried with GCC-9.4.0):

error: variable-sized object may not be initialized

Fix by removing the = {} initialiser and inserting an explicit memset() immediately after the declaration. memset() on a VLA is perfectly legal and achieves the same zero-initialisation on all conforming C implementations.

Quoted 5 lines

This fix is self-contained: it touches only the existing vfio selftest helper library and carries no dependency on any other patch. It was originally included as PATCH 20/20 in the CXL Type-2 VFIO passthrough RFC series [1] but belongs on the vfio list independently, as noted by Dave Jiang.

This should go after the "---" below. It does not need to be in the commit message.

Quoted 1 line

[1] https://lore.kernel.org/all/[email protected]/

I see you also included a new selftest in this series. I'm glad to see more usage of VFIO selftests!

Can you please Cc me on any future changes that touch tools/testing/selftests/vfio? Or better yet run scripts/get_maintainer.pl and it should add me automatically. Thanks.

Quoted 4 lines
Fixes 19faf6fd969c ("vfio: selftests: Add a helper library for VFIO selftests")

Aside from the commit message nits above: