summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2023-10-30 18:07:32 +0100
committerThomas Haller <thaller@redhat.com>2023-10-30 18:07:32 +0100
commit67faab3f4d8539732bed0ea64e48dd9b1a6e6d4b (patch)
treeb169347f967a3ea91bb553383619b3129bb0252f
parentb0d606b427018116f91eac248124f33ab25664ff (diff)
parent563a18513514f8eeb2d83849bbcbd110e4888b8c (diff)
all: merge branch 'th/nm-g-array-index'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1776
-rw-r--r--src/libnm-core-impl/nm-setting.c4
-rw-r--r--src/libnm-glib-aux/nm-enum-utils.c7
-rw-r--r--src/libnm-glib-aux/nm-shared-utils.h22
-rw-r--r--src/nmcli/gen-metadata-nm-settings-nmcli.c2
-rw-r--r--src/nmtui/nmt-8021x-fields.c2
5 files changed, 29 insertions, 8 deletions
diff --git a/src/libnm-core-impl/nm-setting.c b/src/libnm-core-impl/nm-setting.c
index 276dfd3475..efbdc3f5da 100644
--- a/src/libnm-core-impl/nm-setting.c
+++ b/src/libnm-core-impl/nm-setting.c
@@ -355,7 +355,7 @@ _nm_setting_class_commit(NMSettingClass *setting_class,
guint k;
nm_assert(!_nm_sett_info_property_find_in_array(
- nm_g_array_index_p(properties_override, NMSettInfoProperty, 0),
+ nm_g_array_first_p(properties_override, NMSettInfoProperty),
i,
p->name));
for (k = 0; k < n_property_specs; k++) {
@@ -374,7 +374,7 @@ _nm_setting_class_commit(NMSettingClass *setting_class,
NMSettInfoProperty *p;
if (_nm_sett_info_property_find_in_array(
- nm_g_array_index_p(properties_override, NMSettInfoProperty, 0),
+ nm_g_array_first_p(properties_override, NMSettInfoProperty),
override_len,
name))
continue;
diff --git a/src/libnm-glib-aux/nm-enum-utils.c b/src/libnm-glib-aux/nm-enum-utils.c
index 0d63278045..212c0021e0 100644
--- a/src/libnm-glib-aux/nm-enum-utils.c
+++ b/src/libnm-glib-aux/nm-enum-utils.c
@@ -325,7 +325,7 @@ _nm_utils_enum_get_values(GType type, int from, int to)
GPtrArray *values = g_ptr_array_sized_new(values_full->len + 1);
for (i = 0; i < values_full->len; i++) {
- NMUtilsEnumValueInfoFull *v = &g_array_index(values_full, NMUtilsEnumValueInfoFull, i);
+ NMUtilsEnumValueInfoFull *v = &nm_g_array_index(values_full, NMUtilsEnumValueInfoFull, i);
g_ptr_array_add(values, (gpointer) v->nick);
}
@@ -418,8 +418,9 @@ _nm_utils_enum_get_values_full(GType type,
g_array_set_clear_func(array, (GDestroyNotify) _free_value_info_full);
for (i = 0; i < array->len; i++) {
- NMUtilsEnumValueInfoFull *vi_full = &g_array_index(array, NMUtilsEnumValueInfoFull, i);
- GPtrArray *aliases = g_ptr_array_new();
+ NMUtilsEnumValueInfoFull *vi_full =
+ &nm_g_array_index(array, NMUtilsEnumValueInfoFull, i);
+ GPtrArray *aliases = g_ptr_array_new();
const NMUtilsEnumValueInfo *vi;
for (vi = value_infos; vi && vi->nick; vi++) {
diff --git a/src/libnm-glib-aux/nm-shared-utils.h b/src/libnm-glib-aux/nm-shared-utils.h
index 941b7fd986..fb88578b28 100644
--- a/src/libnm-glib-aux/nm-shared-utils.h
+++ b/src/libnm-glib-aux/nm-shared-utils.h
@@ -1941,7 +1941,27 @@ nm_g_array_unref(GArray *arr)
* When accessing index zero, then this returns NULL if-and-only-if
* "arr" is NULL or "arr->data" is NULL. In all other cases, this
* returns the pointer &((Type*) arr->data)[idx]. Note that the pointer
- * may not be followed, if "idx" is equal to "arr->len". */
+ * may not be followed, if "idx" is equal to "arr->len".
+ *
+ * The reason to allow access one element past the length is the
+ * following usage:
+ *
+ * ptr = nm_g_array_index_p(arr, Type, 0);
+ * end = nm_g_array_index_p(arr, Type, length);
+ * for (; ptr < end; ptr++) { ... }
+ *
+ * Another usage is to get a buffer, if the length might be zero. If
+ * length is zero, you cannot dereference the pointer, but it can be convenient
+ * to not require special casing:
+ *
+ * // length might be zero.
+ * nm_memdup(nm_g_array_index_p(arr, Type, length), sizeof(Type) * length);
+ *
+ * Note that in C, it's valid point one past the end of an array. So getting
+ * a pointer at index "length" is valid, and what nm_g_array_index_p() allows.
+ * If you don't need that, nm_g_array_index() is usually preferable,
+ * because it asserts against access at index "length".
+ */
#define nm_g_array_index_p(arr, Type, idx) \
({ \
const GArray *const _arr_55 = (arr); \
diff --git a/src/nmcli/gen-metadata-nm-settings-nmcli.c b/src/nmcli/gen-metadata-nm-settings-nmcli.c
index f8dc303628..2def909565 100644
--- a/src/nmcli/gen-metadata-nm-settings-nmcli.c
+++ b/src/nmcli/gen-metadata-nm-settings-nmcli.c
@@ -266,7 +266,7 @@ _append_enum_valid_values(GType g_type,
GArray *values = _nm_utils_enum_get_values_full(g_type, min, max, value_infos);
for (i = 0; i < values->len; i++) {
- NMUtilsEnumValueInfoFull *val = &g_array_index(values, NMUtilsEnumValueInfoFull, i);
+ NMUtilsEnumValueInfoFull *val = &nm_g_array_index(values, NMUtilsEnumValueInfoFull, i);
g_string_assign(names, val->nick);
diff --git a/src/nmtui/nmt-8021x-fields.c b/src/nmtui/nmt-8021x-fields.c
index 3a2185cfbb..a323be62d7 100644
--- a/src/nmtui/nmt-8021x-fields.c
+++ b/src/nmtui/nmt-8021x-fields.c
@@ -552,7 +552,7 @@ nmt_8021x_fields_constructed(GObject *object)
entry.id = (char *) eap_method_descs[i].id;
g_array_append_val(entries, entry);
}
- priv->authentication = nmt_newt_popup_new(nm_g_array_index_p(entries, NmtNewtPopupEntry, 0));
+ priv->authentication = nmt_newt_popup_new(nm_g_array_first_p(entries, NmtNewtPopupEntry));
nmt_editor_grid_append(grid, "Authentication", NMT_NEWT_WIDGET(priv->authentication), NULL);
widget = nmt_newt_stack_new();