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.

Backports commit ed63ec0d22ccdce3b2222d9a514423b7fbba3a0d from qemu
This commit is contained in:
Michael S. Tsirkin 2018-03-02 00:09:49 -05:00 committed by Lioncash
parent ac013df0a2
commit 0455644974
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -184,8 +184,20 @@
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#endif #endif
#ifdef _MSC_VER
#define QEMU_IS_ARRAY(x) (x)
#else
/*
* &(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])))
#endif
#ifndef ARRAY_SIZE #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 #endif
void *qemu_try_memalign(size_t alignment, size_t size); void *qemu_try_memalign(size_t alignment, size_t size);