Skip to content

Commit 306dcf5

Browse files
rgetzpcercuei
authored andcommitted
dns_sd: treat linked lists properly when removing things
When we were deleting nodes from the linked list, we accessed the "next", from the node that we just cut out (and free()'ed). Fix by keeping track of the previous node, which is used for the "next" when we cut it out. Signed-off-by: Robin Getz <[email protected]>
1 parent 1264b85 commit 306dcf5

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

dns_sd.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ void port_knock_discovery_data(struct dns_sd_discovery_data **ddata)
215215

216216
void remove_dup_discovery_data(struct dns_sd_discovery_data **ddata)
217217
{
218-
struct dns_sd_discovery_data *d, *ndata, *mdata;
218+
struct dns_sd_discovery_data *d, *ndata, *mdata, *prev;
219219
int i, j;
220220

221221
d = *ddata;
@@ -227,15 +227,26 @@ void remove_dup_discovery_data(struct dns_sd_discovery_data **ddata)
227227
return;
228228

229229
iio_mutex_lock(d->lock);
230+
/* since we are removing nodes in the linked list, we keep track of the
231+
* previous "good" node, so we always can link from the last to the next
232+
*/
230233
for (i = 0, ndata = d; ndata->next != NULL; ndata = ndata->next) {
234+
prev = ndata;
231235
for (j = i + 1, mdata = ndata->next; mdata->next != NULL; mdata = mdata->next) {
232236
if (!strcmp(mdata->hostname, ndata->hostname) &&
233237
!strcmp(mdata->addr_str, ndata->addr_str) &&
234238
mdata->port == ndata->port){
235-
IIO_DEBUG("Removing duplicate in list: '%s'\n",
236-
ndata->hostname);
239+
IIO_DEBUG("Removing duplicate in list: %i '%s' '%s' port:%hu\n",
240+
j, ndata->hostname, ndata->addr_str,
241+
ndata->port);
237242
dnssd_remove_node(&d, j);
243+
/* back up one, so the mdata->next will point to the
244+
* next one to be tested.
245+
*/
246+
mdata = prev;
247+
continue;
238248
}
249+
prev = mdata;
239250
j++;
240251
}
241252
i++;

0 commit comments

Comments
 (0)