Re: [Xen-devel] [PATCH v2 1/2] misc: Replace zero-length arrays with flexible array member (automatic)
On Wed, Mar 04, 2020 at 04:38:15PM +0100, Philippe Mathieu-Daudé wrote: > Description copied from Linux kernel commit from Gustavo A. R. Silva > (see [3]): > > --v-- description start --v-- > > The current codebase makes use of the zero-length array language > extension to the C90 standard, but the preferred mechanism to > declare variable-length types such as these ones is a flexible > array member [1], introduced in C99: > > struct foo { > int stuff; > struct boo array[]; > }; > > By making use of the mechanism above, we will get a compiler > warning in case the flexible array does not occur last in the > structure, which will help us prevent some kind of undefined > behavior bugs from being unadvertenly introduced [2] to the > Linux codebase from now on. > > --^-- description end --^-- > > Do the similar housekeeping in the QEMU codebase (which uses > C99 since commit 7be41675f7cb). > > All these instances of code were found with the help of the > following Coccinelle script: > > @@ > identifier s, m, a; > type t, T; > @@ >struct s { > ... > t m; > - T a[0]; > + T a[]; > }; > @@ > identifier s, m, a; > type t, T; > @@ >struct s { > ... > t m; > - T a[0]; > + T a[]; >} QEMU_PACKED; > > [1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html > [2] > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=76497732932f > [3] > https://git.kernel.org/pub/scm/linux/kernel/git/gustavoars/linux.git/commit/?id=17642a2fbd2c1 > > Inspired-by: Gustavo A. R. Silva > Reviewed-by: David Hildenbrand > Signed-off-by: Philippe Mathieu-Daudé Acked-by: Michael S. Tsirkin Reviewed-by: Michael S. Tsirkin > --- > v2: cocci script updated to not match structures of onlyi > a single flexible array member: > > block/qed.h:106:14: error: flexible array member 'offsets' not allowed in > otherwise empty struct > uint64_t offsets[]; /* in bytes */ >^ > --- > bsd-user/qemu.h | 2 +- > contrib/libvhost-user/libvhost-user.h | 2 +- > hw/m68k/bootinfo.h| 2 +- > hw/scsi/srp.h | 6 +++--- > hw/xen/xen_pt.h | 2 +- > include/hw/acpi/acpi-defs.h | 12 ++-- > include/hw/arm/smmu-common.h | 2 +- > include/hw/i386/intel_iommu.h | 3 ++- > include/hw/virtio/virtio-iommu.h | 2 +- > include/sysemu/cryptodev.h| 2 +- > include/tcg/tcg.h | 2 +- > pc-bios/s390-ccw/bootmap.h| 2 +- > pc-bios/s390-ccw/sclp.h | 2 +- > tests/qtest/libqos/ahci.h | 2 +- > block/linux-aio.c | 2 +- > hw/acpi/nvdimm.c | 6 +++--- > hw/dma/soc_dma.c | 2 +- > hw/i386/x86.c | 2 +- > hw/misc/omap_l4.c | 2 +- > hw/nvram/eeprom93xx.c | 2 +- > hw/rdma/vmw/pvrdma_qp_ops.c | 4 ++-- > hw/usb/dev-network.c | 2 +- > hw/usb/dev-smartcard-reader.c | 4 ++-- > hw/virtio/virtio.c| 4 ++-- > net/queue.c | 2 +- > 25 files changed, 38 insertions(+), 37 deletions(-) > > diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h > index 09e8aed9c7..f8bb1e5459 100644 > --- a/bsd-user/qemu.h > +++ b/bsd-user/qemu.h > @@ -95,7 +95,7 @@ typedef struct TaskState { > struct sigqueue *first_free; /* first free siginfo queue entry */ > int signal_pending; /* non zero if a signal may be pending */ > > -uint8_t stack[0]; > +uint8_t stack[]; > } __attribute__((aligned(16))) TaskState; > > void init_task_state(TaskState *ts); > diff --git a/contrib/libvhost-user/libvhost-user.h > b/contrib/libvhost-user/libvhost-user.h > index 6fc8000e99..f30394fab6 100644 > --- a/contrib/libvhost-user/libvhost-user.h > +++ b/contrib/libvhost-user/libvhost-user.h > @@ -286,7 +286,7 @@ typedef struct VuVirtqInflight { > uint16_t used_idx; > > /* Used to track the state of each descriptor in descriptor table */ > -VuDescStateSplit desc[0]; > +VuDescStateSplit desc[]; > } VuVirtqInflight; > > typedef struct VuVirtqInflightDesc { > diff --git a/hw/m68k/bootinfo.h b/hw/m68k/bootinfo.h > index 5f8ded2686..c954270aad 100644 > --- a/hw/m68k/bootinfo.h > +++ b/hw/m68k/bootinfo.h > @@ -14,7 +14,7 @@ > struct bi_record { > uint16_t tag;/* tag ID */ > uint16_t size; /* size of record */ > -uint32_t data[0];/* data */ > +uint32_t data[]; /* data */ > }; > > /* machine independent tags */ > diff --git a/hw/scsi/srp.h b/hw/scsi/srp.h > index d27f31d2d5..54c954badd 100644 > --- a/hw/scsi/srp.h > +++ b/hw/scsi/srp.h > @@ -112,7 +112,7 @@ struct srp_direct_buf { > struct srp_indirect_buf { > struct srp_direct_buftable_desc; > uint32_t
[Xen-devel] [PATCH v2 1/2] misc: Replace zero-length arrays with flexible array member (automatic)
Description copied from Linux kernel commit from Gustavo A. R. Silva (see [3]): --v-- description start --v-- The current codebase makes use of the zero-length array language extension to the C90 standard, but the preferred mechanism to declare variable-length types such as these ones is a flexible array member [1], introduced in C99: struct foo { int stuff; struct boo array[]; }; By making use of the mechanism above, we will get a compiler warning in case the flexible array does not occur last in the structure, which will help us prevent some kind of undefined behavior bugs from being unadvertenly introduced [2] to the Linux codebase from now on. --^-- description end --^-- Do the similar housekeeping in the QEMU codebase (which uses C99 since commit 7be41675f7cb). All these instances of code were found with the help of the following Coccinelle script: @@ identifier s, m, a; type t, T; @@ struct s { ... t m; - T a[0]; + T a[]; }; @@ identifier s, m, a; type t, T; @@ struct s { ... t m; - T a[0]; + T a[]; } QEMU_PACKED; [1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html [2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=76497732932f [3] https://git.kernel.org/pub/scm/linux/kernel/git/gustavoars/linux.git/commit/?id=17642a2fbd2c1 Inspired-by: Gustavo A. R. Silva Reviewed-by: David Hildenbrand Signed-off-by: Philippe Mathieu-Daudé --- v2: cocci script updated to not match structures of onlyi a single flexible array member: block/qed.h:106:14: error: flexible array member 'offsets' not allowed in otherwise empty struct uint64_t offsets[]; /* in bytes */ ^ --- bsd-user/qemu.h | 2 +- contrib/libvhost-user/libvhost-user.h | 2 +- hw/m68k/bootinfo.h| 2 +- hw/scsi/srp.h | 6 +++--- hw/xen/xen_pt.h | 2 +- include/hw/acpi/acpi-defs.h | 12 ++-- include/hw/arm/smmu-common.h | 2 +- include/hw/i386/intel_iommu.h | 3 ++- include/hw/virtio/virtio-iommu.h | 2 +- include/sysemu/cryptodev.h| 2 +- include/tcg/tcg.h | 2 +- pc-bios/s390-ccw/bootmap.h| 2 +- pc-bios/s390-ccw/sclp.h | 2 +- tests/qtest/libqos/ahci.h | 2 +- block/linux-aio.c | 2 +- hw/acpi/nvdimm.c | 6 +++--- hw/dma/soc_dma.c | 2 +- hw/i386/x86.c | 2 +- hw/misc/omap_l4.c | 2 +- hw/nvram/eeprom93xx.c | 2 +- hw/rdma/vmw/pvrdma_qp_ops.c | 4 ++-- hw/usb/dev-network.c | 2 +- hw/usb/dev-smartcard-reader.c | 4 ++-- hw/virtio/virtio.c| 4 ++-- net/queue.c | 2 +- 25 files changed, 38 insertions(+), 37 deletions(-) diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h index 09e8aed9c7..f8bb1e5459 100644 --- a/bsd-user/qemu.h +++ b/bsd-user/qemu.h @@ -95,7 +95,7 @@ typedef struct TaskState { struct sigqueue *first_free; /* first free siginfo queue entry */ int signal_pending; /* non zero if a signal may be pending */ -uint8_t stack[0]; +uint8_t stack[]; } __attribute__((aligned(16))) TaskState; void init_task_state(TaskState *ts); diff --git a/contrib/libvhost-user/libvhost-user.h b/contrib/libvhost-user/libvhost-user.h index 6fc8000e99..f30394fab6 100644 --- a/contrib/libvhost-user/libvhost-user.h +++ b/contrib/libvhost-user/libvhost-user.h @@ -286,7 +286,7 @@ typedef struct VuVirtqInflight { uint16_t used_idx; /* Used to track the state of each descriptor in descriptor table */ -VuDescStateSplit desc[0]; +VuDescStateSplit desc[]; } VuVirtqInflight; typedef struct VuVirtqInflightDesc { diff --git a/hw/m68k/bootinfo.h b/hw/m68k/bootinfo.h index 5f8ded2686..c954270aad 100644 --- a/hw/m68k/bootinfo.h +++ b/hw/m68k/bootinfo.h @@ -14,7 +14,7 @@ struct bi_record { uint16_t tag;/* tag ID */ uint16_t size; /* size of record */ -uint32_t data[0];/* data */ +uint32_t data[]; /* data */ }; /* machine independent tags */ diff --git a/hw/scsi/srp.h b/hw/scsi/srp.h index d27f31d2d5..54c954badd 100644 --- a/hw/scsi/srp.h +++ b/hw/scsi/srp.h @@ -112,7 +112,7 @@ struct srp_direct_buf { struct srp_indirect_buf { struct srp_direct_buftable_desc; uint32_t len; -struct srp_direct_bufdesc_list[0]; +struct srp_direct_bufdesc_list[]; } QEMU_PACKED; enum { @@ -211,7 +211,7 @@ struct srp_cmd { uint8_treserved4; uint8_tadd_cdb_len; uint8_tcdb[16]; -uint8_tadd_data[0]; +uint8_tadd_data[]; } QEMU_PACKED; enum { @@ -241,7 +241,7 @@ struct srp_rsp { uint32_t data_in_res_cnt; uint32_t s