Index: channel.c =================================================================== --- channel.c (revision 7436) +++ channel.c (working copy) @@ -996,7 +996,22 @@ AST_LIST_HEAD_INIT_NOLOCK(&chan->spies->list); AST_LIST_INSERT_HEAD(&chan->spies->list, spy, list); } else { - AST_LIST_INSERT_TAIL(&chan->spies->list, spy, list); + do { + if ((&chan->spies->list)->first == (spy)) { + (&chan->spies->list)->first = (spy)->list.next; + if ((&chan->spies->list)->last == (spy)) + (&chan->spies->list)->last = NULL; + } else { + struct ast_channel_spy *curelm = (&chan->spies->list)->first; + while (curelm && curelm->list.next != (spy)) + curelm = curelm->list.next; + if (curelm) { + curelm->list.next = (spy)->list.next; + if ((&chan->spies->list)->last == (spy)) + (&chan->spies->list)->last = curelm; + } + } + } while (0); } if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE) { Index: include/asterisk/linkedlists.h =================================================================== --- include/asterisk/linkedlists.h (revision 7436) +++ include/asterisk/linkedlists.h (working copy) @@ -381,7 +381,8 @@ used to link entries of this list together. Note: The link field in the appended entry is \b not modified, so if it is - actually the head of a list itself, the entire list will be appended. + actually the head of a list itself, the entire list will be appended + temporarily (until the next AST_LIST_INSERT_TAIL is performed). */ #define AST_LIST_INSERT_TAIL(head, elm, field) do { \ if (!(head)->first) { \ @@ -428,11 +429,13 @@ (head)->last = NULL; \ } else { \ typeof(elm) curelm = (head)->first; \ - while (curelm->field.next != (elm)) \ + while (curelm && curelm->field.next != (elm)) \ curelm = curelm->field.next; \ - curelm->field.next = (elm)->field.next; \ - if ((head)->last == curelm->field.next) \ - (head)->last = curelm; \ + if (curelm) { /* Found in list */ \ + curelm->field.next = (elm)->field.next; \ + if ((head)->last == (elm)) \ + (head)->last = curelm; \ + } /* else not found */ \ } \ } while (0)