Index | Thread | Search

From:
Nick Owens <mischief@offblast.org>
Subject:
fix STAILQ_INSERT_AFTER
To:
tech@openbsd.org
Date:
Wed, 10 Jun 2026 07:45:00 -0700

Download raw body.

Thread
  • Nick Owens:

    fix STAILQ_INSERT_AFTER

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 {                            \