title: Why doesn't TAILQ_REMOVE reset the head pointer?
body: <p>I'm tracking down a few odd Coverity bugs in some code I didn't write.  In one case, we use TAILQ_FIRST and TAILQ_REMOVE in a loop, like so:</p>
  
  <pre><code>while (!TAILQ_EMPTY(&amp;queue))
  {
      item* entry = TAILQ_FIRST(&amp;queue);
      TAILQ_REMOVE(&amp;queue, entry, next);
      free(entry);
  }
  </code></pre>
  
  <p>Coverity complains a lot about this, saying I'm double-freeing.  However, looking at TAILQ_REMOVE, this might be right:  (<code>/usr/include/x86_64-linux-gnu/sys/queue.h</code> on my Linux box)</p>
  
  <pre><code>#define TAILQ_REMOVE(head, elm, field) do {                             \
          if (((elm)-&gt;field.tqe_next) != NULL)                            \
                  (elm)-&gt;field.tqe_next-&gt;field.tqe_prev =                 \
                      (elm)-&gt;field.tqe_prev;                              \
          else                                                            \
                  (head)-&gt;tqh_last = (elm)-&gt;field.tqe_prev;               \
          *(elm)-&gt;field.tqe_prev = (elm)-&gt;field.tqe_next;                 \
  } while (/*CONSTCOND*/0)
  </code></pre>
  
  <p>Unlike in some other related macros, I don't see anything here that resets <code>tqe_first</code> if I delete the head node.  Thus, I would keep getting the deleted node in my loop.</p>
  
  <p>But I don't really understand what's going on.  This code appears to work despite the Coverity warnings.  </p>
  
  <p>Finding examples on the net for this is difficult.  </p>