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().
This commit is contained in:
Thomas Haller 2023-10-25 20:33:26 +02:00
parent 208381f78b
commit 0554ef3808
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 5 additions and 4 deletions

View file

@ -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++) {

View file

@ -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);