From: Gaetan Rivet <[email protected]>

Implicit padding bytes in a struct are not necessarily initialized.
Comparing such struct with memcmp might be wrong. To verify correctness
of such comparison, add a macro to statically assert whether a structure
contains padding.

Signed-off-by: Gaetan Rivet <[email protected]>
Co-authored-by: Eli Britstein <[email protected]>
Signed-off-by: Eli Britstein <[email protected]>
---
 include/openvswitch/compiler.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/include/openvswitch/compiler.h b/include/openvswitch/compiler.h
index 7dee9ec7a..961339944 100644
--- a/include/openvswitch/compiler.h
+++ b/include/openvswitch/compiler.h
@@ -218,6 +218,18 @@
 #define OVS_PACKED(DECL) __pragma(pack(push, 1)) DECL __pragma(pack(pop))
 #endif
 
+/* Define a type with this macro to enforce during
+ * compilation that it has no padding, i.e. packed.
+ * It *DOES NOT* pack the type, only statically check
+ * that it is already. Either the layout must be
+ * made to fit, or explicit padding must be used.
+ */
+#define OVS_ASSERT_PACKED(TYPE, MEMBERS) \
+    TYPE { \
+        MEMBERS \
+    }; \
+    BUILD_ASSERT_DECL(sizeof(TYPE) == sizeof(OVS_PACKED(struct { MEMBERS })))
+
 /* OVS_ALIGNED_STRUCT may be used to define a structure whose instances should
  * aligned on an N-byte boundary.  This:
  *     OVS_ALIGNED_STRUCT(64, mystruct) { ... };
-- 
2.34.1

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to