Correction for discarding the last cookie in the queue
After the for loop, last->next will have been NULL which makes no sense in the calls to cookie_unlink_cookie and destroy_cookie.
Also cookie_unlink_cookie expects the first parameter to be a pointer to the variable where the head of the queue is stored, not a pointer to the cookie to be discarded.
This bug was discussed on a Forum thread.