[PATCH 20/20] selftests/vfio: Fix VLA initialisation in vfio_pci_irq_set()

mhonap@nvidia.com posted 20 patches 3 weeks, 5 days ago
There is a newer version of this series
[PATCH 20/20] selftests/vfio: Fix VLA initialisation in vfio_pci_irq_set()
Posted by mhonap@nvidia.com 3 weeks, 5 days ago
From: Manish Honap <mhonap@nvidia.com>

C does not permit initialiser expressions on variable-length arrays.
vfio_pci_irq_set() declared

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

where count is a function parameter, making buf a VLA.  GCC rejects
this with "variable-sized object may not be initialized".

Replace the initialiser with an explicit memset() immediately after
the declaration.

Fixes: 19faf6fd969c2 ("vfio: selftests: Add a helper library for VFIO selftests")
Signed-off-by: Manish Honap <mhonap@nvidia.com>
---
 tools/testing/selftests/vfio/lib/vfio_pci_device.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
index fac4c0ecadef..3258e814f450 100644
--- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
+++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
@@ -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
Re: [PATCH 20/20] selftests/vfio: Fix VLA initialisation in vfio_pci_irq_set()
Posted by Dave Jiang 3 weeks, 3 days ago

On 3/11/26 1:34 PM, mhonap@nvidia.com wrote:
> From: Manish Honap <mhonap@nvidia.com>
> 
> C does not permit initialiser expressions on variable-length arrays.
> vfio_pci_irq_set() declared
> 
> u8 buf[sizeof(struct vfio_irq_set) + sizeof(int) * count] = {};
> 
> where count is a function parameter, making buf a VLA.  GCC rejects
> this with "variable-sized object may not be initialized".
> 
> Replace the initialiser with an explicit memset() immediately after
> the declaration.
> 
> Fixes: 19faf6fd969c2 ("vfio: selftests: Add a helper library for VFIO selftests")

Should this fix be split out from the series and sent ahead? Does not seem to be tied to the current implementation. 

DJ

> Signed-off-by: Manish Honap <mhonap@nvidia.com>
> ---
>  tools/testing/selftests/vfio/lib/vfio_pci_device.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
> index fac4c0ecadef..3258e814f450 100644
> --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
> +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
> @@ -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);