[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL v4 08/23] ARRAY_SIZE: check that argument is an array
From: |
Michael S. Tsirkin |
Subject: |
[Qemu-devel] [PULL v4 08/23] ARRAY_SIZE: check that argument is an array |
Date: |
Tue, 31 Jan 2017 20:43:09 +0200 |
It's a familiar pattern: some code uses ARRAY_SIZE, then refactoring
changes the argument from an array to a pointer to a dynamically
allocated buffer. Code keeps compiling but any ARRAY_SIZE calls now
return the size of the pointer divided by element size.
Let's add build time checks to ARRAY_SIZE before we allow more
of these in the code-base.
Signed-off-by: Michael S. Tsirkin <address@hidden>
Reviewed-by: Markus Armbruster <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
---
include/qemu/osdep.h | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index 689f253..56c9e22 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -198,8 +198,15 @@ extern int daemon(int, int);
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#endif
+/*
+ * &(x)[0] is always a pointer - if it's same type as x then the argument is a
+ * pointer, not an array.
+ */
+#define QEMU_IS_ARRAY(x) (!__builtin_types_compatible_p(typeof(x), \
+ typeof(&(x)[0])))
#ifndef ARRAY_SIZE
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+#define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0])) + \
+ QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(x)))
#endif
int qemu_daemon(int nochdir, int noclose);
--
MST
- [Qemu-devel] [PULL v4 00/23] virtio, vhost, pci: fixes, features, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 01/23] compiler: drop ; after BUILD_BUG_ON, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 02/23] qxl: switch to constants within BUILD_BUG_ON, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 03/23] ppc: switch to constants within BUILD_BUG_ON, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 05/23] compiler: rework BUG_ON using a struct, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 04/23] QEMU_BUILD_BUG_ON: use __COUNTER__, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 06/23] QEMU_BUILD_BUG_ON: use __COUNTER__, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 08/23] ARRAY_SIZE: check that argument is an array,
Michael S. Tsirkin <=
- [Qemu-devel] [PULL v4 07/23] compiler: expression version of QEMU_BUILD_BUG_ON, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 09/23] pci: mark ROMs read-only, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 10/23] intel_iommu: fix and simplify size calculation in process_device_iotlb_desc(), Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 12/23] hw/ioh3420: derive from PCI Express Root Port base class, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 11/23] hw/pcie: Introduce a base class for PCI Express Root Ports, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 13/23] hw/pcie: Introduce Generic PCI Express Root Port, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 14/23] hw/i386: check if nvdimm is enabled before plugging, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 15/23] msix: Follow CODING_STYLE, Michael S. Tsirkin, 2017/01/31
- [Qemu-devel] [PULL v4 16/23] hcd-xhci: check & correct param before using it, Michael S. Tsirkin, 2017/01/31