fix STAILQ_INSERT_AFTER

Nick Owens <[email protected]>
Newsgroups gmane.os.openbsd.tech
Message-ID <[email protected]>
hi,

this fixes STAILQ_INSERT_AFTER. right now it's more like a no-op. i
suppose it went unnoticed because nothing i found uses it.

here is my test program:

#include <stdio.h>
#include <sys/queue.h>

struct item {
    int val;
    STAILQ_ENTRY(item) next;
};
STAILQ_HEAD(itemlist, item);

int main(void) {
    struct itemlist list = STAILQ_HEAD_INITIALIZER(list);
    struct item a = {.val = 1}, b = {.val = 2}, c = {.val = 3};

    /* Insert a at head */
    STAILQ_INSERT_HEAD(&list, &a, next);

    /* Insert b after a */
    STAILQ_INSERT_AFTER(&list, &a, &b, next);

    /* Insert c after b */
    STAILQ_INSERT_AFTER(&list, &b, &c, next);

    /* Print */
    struct item *it;
    int n = 0;
    STAILQ_FOREACH(it, &list, next) {
        printf("[%d] val=%d\n", n++, it->val);
    }
    printf("total: %d (expected 3)\n", n);
    return n == 3 ? 0 : 1;
}

diff --git a/sys/sys/queue.h b/sys/sys/queue.h
index 20b78871415..2f1c1c661c6 100644
--- a/sys/sys/queue.h
+++ b/sys/sys/queue.h
@@ -589,9 +589,9 @@ struct {								\
 } while (0)
 
 #define	STAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
-	if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((elm), field)) == NULL)\
+	if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((listelm), field)) == NULL)\
 		(head)->stqh_last = &STAILQ_NEXT((elm), field);		\
-	STAILQ_NEXT((elm), field) = (elm);				\
+	STAILQ_NEXT((listelm), field) = (elm);				\
 } while (0)
 
 #define STAILQ_REMOVE_HEAD(head, field) do {                            \
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.