From 0554ef380863618e02f08fa614fde50f49eeb2ad Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 25 Oct 2023 20:33:26 +0200 Subject: [PATCH] all: use nm_g_array_index() instead of g_array_index() nm_g_array_index() performs additional nm_assert() checks for correctness. In this case, it's pretty clear that the assertion will hold and that the code is correct. Note that most of the time when having assertions, we expect that they hold. Since nm_assert() is disabled in release build, arguing that an assertion holds is not a strong argument against having the assert (they are always supposed to hold, quite obviously so!). The reason to change is that we should use the wrappers that perform additional checks. Especially when the additional checks are nm_assert() or static-asserts, as they are not present in release builds. To find how well we are doing in this regard we can check `git grep -w g_array_index`. If that gives many uses of the unchecked function, then we cannot manually check them all to be really obviously correct. Instead, we should not use g_array_index() and trivially see that all array accesses are guarded by assertions. "checkpatch.pl" also recommends against g_array_index(). --- src/libnm-glib-aux/nm-enum-utils.c | 7 ++++--- src/nmcli/gen-metadata-nm-settings-nmcli.c | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) 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/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);