From e60c52829c6256237645ca6aa7cbb3576f9e4453 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 17 Aug 2021 19:18:42 +0200 Subject: [PATCH] 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). (cherry picked from commit 53070705b0f033aed4c7891b7e0eb19534c1c8d7) --- src/libnm-glib-aux/nm-dedup-multi.h | 8 ++++++-- 1 file 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). */