diff mbox series

[1/1] cache: fix AVL tree traversal in cache_record_find() and cache_host_is_known()

Message ID 20220203082230.57497-2-mroeder@metz-connect.com
State New
Headers show
Series Bugfix for OpenWrt package umdns | expand

Commit Message

MRoeder@metz-connect.com Feb. 3, 2022, 8:22 a.m. UTC
From: Martin Röder <mroeder@metz-connect.com>

The AVL tree traversal in both functions systematically misses the last
AVL tree element. This can lead to duplicate cache entries and lookup failures.

The fix duplicates the correct AVL tree traversal approach of cache_dump_recursive().

Signed-off-by: Martin Röder <mroeder@metz-connect.com>
---
 cache.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/cache.c b/cache.c
index ea6a4c8..d1816df 100644
--- a/cache.c
+++ b/cache.c
@@ -191,13 +191,10 @@  cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata
 {
 	struct cache_record *l = avl_find_element(&records, record, l, avl);
 
-	if (!l)
-		return NULL;
-
-	while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
+	while (l && !strcmp(l->record, record)) {
 		struct cache_record *r = l;
 
-		l = avl_next_element(l, avl);
+		l = !avl_is_last(&records, &l->avl) ? avl_next_element(l, avl) : NULL;
 		if (r->type != type)
 			continue;
 
@@ -227,13 +224,10 @@  cache_host_is_known(char *record)
 {
 	struct cache_record *l = avl_find_element(&records, record, l, avl);
 
-	if (!l)
-		return 0;
-
-	while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
+	while (l && !strcmp(l->record, record)) {
 		struct cache_record *r = l;
 
-		l = avl_next_element(l, avl);
+		l = !avl_is_last(&records, &l->avl) ? avl_next_element(l, avl) : NULL;
 		if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
 			continue;
 		return 1;