summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2021-06-07 17:27:53 +0200
committerThomas Haller <thaller@redhat.com>2021-06-07 17:33:02 +0200
commitddd6587a6f323353b57eb729b6ae00b7aa363d76 (patch)
tree98690a9ed8ccda8c6d3039deb7fb7828dd666619
parentb92d8f5a2de403ef4de262f2f42258cec8a28f91 (diff)
downloadNetworkManager-ddd6587a6f323353b57eb729b6ae00b7aa363d76.tar.gz
libnm: assert initialization in _NM_OBJECT_CLASS_INIT_FIELD_INFO()
_NM_OBJECT_CLASS_INIT_FIELD_INFO() is a bit odd, because it defines a static variable and initialized it at the moment when being "called". This is in fact correct, because this code only gets called from inside the _class_init() function, which is executed at most once. Add an assertion to ensure that the static variables is not yet initialized.
-rw-r--r--src/libnm-client-impl/nm-libnm-utils.h38
1 files changed, 23 insertions, 15 deletions
diff --git a/src/libnm-client-impl/nm-libnm-utils.h b/src/libnm-client-impl/nm-libnm-utils.h
index 3fd3b679e8..ef80bb4292 100644
--- a/src/libnm-client-impl/nm-libnm-utils.h
+++ b/src/libnm-client-impl/nm-libnm-utils.h
@@ -758,21 +758,29 @@ struct _NMObjectClass {
} \
G_STMT_END
-#define _NM_OBJECT_CLASS_INIT_FIELD_INFO(_nm_object_class, _field_name, _offset, _num) \
- G_STMT_START \
- { \
- (_nm_object_class)->_field_name = ({ \
- static _NMObjectClassFieldInfo _f; \
- \
- _f = (_NMObjectClassFieldInfo){ \
- .parent = (_nm_object_class)->_field_name, \
- .klass = (_nm_object_class), \
- .offset = _offset, \
- .num = _num, \
- }; \
- &_f; \
- }); \
- } \
+#define _NM_OBJECT_CLASS_INIT_FIELD_INFO(nm_object_class, field_name, _offset, _num) \
+ G_STMT_START \
+ { \
+ NMObjectClass *const _klass = (nm_object_class); \
+ \
+ _klass->field_name = ({ \
+ static _NMObjectClassFieldInfo _f; \
+ \
+ /* this code is called inside a _class_init() function, it thus
+ * is only executed once, so we are fine to initialize a static
+ * variable here. */ \
+ nm_assert(!_f.klass); \
+ \
+ _f = (_NMObjectClassFieldInfo){ \
+ .parent = _klass->field_name, \
+ .klass = _klass, \
+ .offset = (_offset), \
+ .num = (_num), \
+ }; \
+ \
+ &_f; \
+ }); \
+ } \
G_STMT_END
#define _NM_OBJECT_CLASS_INIT_PROPERTY_O_FIELDS_1(nm_object_class, type_name, field_name) \