Maapi_delete() while iterating with maapi_get_next()

Hi all,

Is the following code snippet safe?

Trying to iterate and delete rows which matches one of the keys.

struct maapi_cursor mc;
maapi_init_cursor(msock, th, &mc, path);
maapi_get_next(&mc);

while (mc.n)
{
    if (CONFD_GET_XXX(&mc.keys[0]) == somevalue)
   {
       // Delete row
       maapi_delete(msock, th, path"{%x %x}", &mc.keys[0], &mc.keys[1]); 
   }
   maapi_get_next(&mc);
}

In one case, we are seeing there are multiple entries with the matching condition, but the iteration code deletes only one row.

But we have this code pattern in multiple places but similar issues were not reported before. That makes me wonder whether the above pattern is safe or not.

Regards,

i haven’t tested/checked it myself, but this might be problematic just like many other instances of use-case when collection is being modified under the hands of iterator… (cursor in our case)

Assuming your condition is to delete all entries with specific key[0] value as the code excerpt shows, maybe you could change your code to following pattern?

  • use maapi_find_next() with newly initialized cursor to find first item that matches your specific key[0]
  • if found, delete it & repeat maapi_find_next() with new cursor from start again