qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 2/5] qemu/qarray.h: weak scalar type check in QARRAY_CREATE()


From: Richard Henderson
Subject: Re: [PATCH 2/5] qemu/qarray.h: weak scalar type check in QARRAY_CREATE()
Date: Sat, 21 Aug 2021 21:11:58 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0

On 8/21/21 1:30 PM, Christian Schoenebeck wrote:
Unfortunately something like

   _Static_assert(typeof(a) == typeof(b), "type mismatch");

is currently not suported by C. So for the time being at least
check that the size of the scalar types match at compile time.

Did you try
_Static_assert(__builtin_types_compatible_p(X, Y), "type mismatch");


r~


Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
  include/qemu/qarray.h | 6 ++++++
  1 file changed, 6 insertions(+)

diff --git a/include/qemu/qarray.h b/include/qemu/qarray.h
index 230a556e81..2cb8656c5d 100644
--- a/include/qemu/qarray.h
+++ b/include/qemu/qarray.h
@@ -27,6 +27,8 @@
  #ifndef QEMU_QARRAY_H
  #define QEMU_QARRAY_H
+#include "qemu/compiler.h"
+
  /**
   * QArray provides a mechanism to access arrays in common C-style (e.g. by
   * square bracket [] operator) in conjunction with reference variables that
@@ -143,6 +145,10 @@
   * @param len - amount of array elements to be allocated immediately
   */
  #define QARRAY_CREATE(scalar_type, auto_var, len) \
+    QEMU_BUILD_BUG_MSG( \
+        sizeof(scalar_type) != sizeof(*auto_var), \
+        "QArray scalar type mismatch" \
+    ); \
      qarray_create_##scalar_type((&auto_var), len)
#endif /* QEMU_QARRAY_H */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]