Fix ptree_for_each_element loop termination condition

Condition '&a->n != NULL' tests address of struct member, always non-NULL
for valid pointers. When ptree_get_next() returns NULL, ptree_element(NULL, t, n)
produces pointer at -offsetof(t,n), non-NULL unless n is first member.

Check pointer itself for NULL instead of member address.

Fixes #213
This commit is contained in:
Levi Neely 2026-03-03 15:28:48 +01:00
parent ac25de2831
commit 6bb62223c8
1 changed files with 3 additions and 2 deletions

View File

@ -49,8 +49,9 @@ typedef struct _psync_tree {
#define ptree_for_each(a, l) \
for (a = ptree_get_first(l); a != NULL; a = ptree_get_next(a))
#define ptree_for_each_element(a, l, t, n) \
for (a = ptree_element(ptree_get_first(l), t, n); &a->n != NULL; \
a = ptree_element(ptree_get_next(&a->n), t, n))
for (a = (ptree_get_first(l) ? ptree_element(ptree_get_first(l), t, n) : NULL); \
a != NULL; \
a = (ptree_get_next(&a->n) ? ptree_element(ptree_get_next(&a->n), t, n) : NULL))
#define ptree_for_each_element_call(l, t, n, c) \
do { \