summaryrefslogtreecommitdiffstats
path: root/include/qemu/osdep.h
diff options
context:
space:
mode:
authorMichael S. Tsirkin2017-01-18 21:07:34 +0100
committerMichael S. Tsirkin2017-02-01 02:37:17 +0100
commited63ec0d22ccdce3b2222d9a514423b7fbba3a0d (patch)
treee8c361e656f9e039332374484dd1f8607317a219 /include/qemu/osdep.h
parentcompiler: expression version of QEMU_BUILD_BUG_ON (diff)
downloadqemu-ed63ec0d22ccdce3b2222d9a514423b7fbba3a0d.tar.gz
qemu-ed63ec0d22ccdce3b2222d9a514423b7fbba3a0d.tar.xz
qemu-ed63ec0d22ccdce3b2222d9a514423b7fbba3a0d.zip
ARRAY_SIZE: check that argument is an array
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 <mst@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'include/qemu/osdep.h')
-rw-r--r--include/qemu/osdep.h9
1 files changed, 8 insertions, 1 deletions
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index 689f253ea7..56c9e22405 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);