Skip to content

Support IT_REMOVE ops inside M_EACH() - #173

Closed
WillyJL wants to merge 1 commit into
P-p-H-d:masterfrom
WillyJL:fix/m-each-remove
Closed

WillyJL wants to merge 1 commit into
P-p-H-d:masterfrom
WillyJL:fix/m-each-remove

Conversation

@WillyJL

@WillyJL WillyJL commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

currently, using M_EACH() with a container such as M_LIST and using _remove() on the last item will lead to a failed assert, as the iterator can no longer progress.

this makes it so it will check if the iterator already reached the end during this iteration before trying to step it.

@P-p-H-d

P-p-H-d commented Nov 9, 2026

Copy link
Copy Markdown
Owner

I'm not sure if it is a proper fix.
Can you provide an example of failing code?

@WillyJL

WillyJL commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Example of failing code:

#include <mlib/m-list.h>
#include <mlib/m-string.h>

M_LIST_DUALF_DEF(m_string_list, m_string_t)
#define M_OPL_m_string_list_t() M_LIST_OPLIST(m_string_list, M_OPL_m_string_t())

int main() {
    m_string_list_t args;
    m_string_list_init(args);
    m_string_set(*m_string_list_push_back_new(args), "/usr/bin/chromium");
    m_string_set(*m_string_list_push_back_new(args), "%U");

    for
        M_EACH(arg, args, M_OPL_m_string_list_t()) {
            if(m_string_start_with_str_p(*arg, "%")) {
                m_string_list_remove(args, local_iterator_14);
            }
        }

    return 0;
}

which crashes with:

void m_string_list_next(struct m_string_list_it_s *): Assertion `it != ((void*)0) && it->current != ((void*)0)' failed.

I realized now writing the example code that maybe this might not be as necessary upstream: I am using a modified version of M_EACH() in my app with the iterator name more accessible (in the above example, it would be arg_it instead of local_iterator_14) to avoid having to make custom iterator loops with extra checks, so I ran into this issue as I'm using the iterator directly. I guess local_iterator_<line> isn't really meant to be used by end users, so maybe this doesn't need fixing?

@P-p-H-d

P-p-H-d commented Nov 11, 2026

Copy link
Copy Markdown
Owner

local_iterator_ isn't meant to be used by end users; and the fix you propose is also flawed as when you remove an item you advance your iterator by 2 instead of 1 (IT_REMOVE already advances the iterator to the next item).

@WillyJL

WillyJL commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Ah I see, thanks for the heads up

@WillyJL WillyJL closed this Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants