From a79cd9da977ac2eb7ac09bf3a905b650538b1658 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Mon, 5 Jul 2021 22:44:58 +0200 Subject: [PATCH] Fix issue for RB Tree _it_equal_p method if both iterators are equal and are associated to a RB tree without any element. --- m-rbtree.h | 9 +++++++-- tests/test-mrbtree.c | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/m-rbtree.h b/m-rbtree.h index 55365cd4..fbfea9c8 100644 --- a/m-rbtree.h +++ b/m-rbtree.h @@ -540,7 +540,9 @@ typedef enum { static inline type * \ M_C(name, _ref)(const it_t it) \ { \ - M_ASSERT(it != NULL && it->cpt > 0); \ + M_ASSERT(it != NULL); \ + /* There shall be at least one element */ \ + M_ASSERT_INDEX(it->cpt-1, M_RBTR33_MAX_STACK); \ /* NOTE: partially unsafe if the user modify the order of the el */ \ return &(it->stack[it->cpt-1]->data); \ } \ @@ -555,8 +557,11 @@ typedef enum { M_C(name, _it_equal_p)(const it_t it1, const it_t it2) \ { \ M_ASSERT(it1 != NULL && it2 != NULL); \ + /* There can be no element */ \ + M_ASSERT_INDEX(it1->cpt, M_RBTR33_MAX_STACK); \ + M_ASSERT_INDEX(it2->cpt, M_RBTR33_MAX_STACK); \ return it1->cpt == it2->cpt \ - && it1->stack[it1->cpt-1] == it2->stack[it2->cpt-1]; \ + && (it1->cpt == 0 || it1->stack[it1->cpt-1] == it2->stack[it2->cpt-1]); \ } \ \ \ diff --git a/tests/test-mrbtree.c b/tests/test-mrbtree.c index 51a9f038..59471696 100644 --- a/tests/test-mrbtree.c +++ b/tests/test-mrbtree.c @@ -96,6 +96,8 @@ static void test_uint(void) rbtree_uint_it_end(it, tree); assert (rbtree_uint_end_p(it)); assert(!rbtree_uint_it_equal_p(it2, it)); + rbtree_uint_it_end(it2, tree); + assert(rbtree_uint_it_equal_p(it2, it)); assert( k == 0); assert (rbtree_uint_hash(tree) != 0);