diff options
author | Thomas Haller <thaller@redhat.com> | 2021-08-17 19:18:42 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2021-08-17 19:56:38 +0200 |
commit | 53070705b0f033aed4c7891b7e0eb19534c1c8d7 (patch) | |
tree | f02304ed8c2c4a86e377bafd2831ee0e8d01b770 | |
parent | 57a519cc035a55ed51c5e5fd1390eb387d342e8f (diff) | |
download | NetworkManager-53070705b0f033aed4c7891b7e0eb19534c1c8d7.tar.gz |
glib-aux: clear iterator in nm_dedup_multi_iter_{next,prev}() at the end
It seems slightly nicer not to leave a dangling pointer at the
end of the iteration. Then you could do something like
nm_dedup_multi_iter_init(&iter, head_entry);
while (nm_dedup_multi_iter_next(&iter)) {
if (some_condition())
break;
}
if (!iter.current)
printf("iterated to the end\n");
As nm_dedup_multi_iter_next() and nm_dedup_multi_iter_init() are inline
functions, the compiler should even be able to see that the initial
setting becomes unnecessary (the field will be initialized by the
first nm_dedup_multi_iter_next()). Likewise, the final clearing
of the field might also be optimized away at the end of the iteration
(if, as in the common case, the iterator is not accessed afterwards).
-rw-r--r-- | src/libnm-glib-aux/nm-dedup-multi.h | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/libnm-glib-aux/nm-dedup-multi.h b/src/libnm-glib-aux/nm-dedup-multi.h index 75cc193363..fb1cb86340 100644 --- a/src/libnm-glib-aux/nm-dedup-multi.h +++ b/src/libnm-glib-aux/nm-dedup-multi.h @@ -352,8 +352,10 @@ nm_dedup_multi_iter_next(NMDedupMultiIter *iter) { g_return_val_if_fail(iter, FALSE); - if (!iter->_next) + if (!iter->_next) { + iter->current = NULL; return FALSE; + } /* we always look ahead for the next. This way, the user * may delete the current entry (but no other entries). */ @@ -370,8 +372,10 @@ nm_dedup_multi_iter_prev(NMDedupMultiIter *iter) { g_return_val_if_fail(iter, FALSE); - if (!iter->_prev) + if (!iter->_prev) { + iter->current = NULL; return FALSE; + } /* we always look ahead for the prev. This way, the user * may delete the current entry (but no other entries). */ |