From 5cf68dca6a903f5339d582b87e7b95acb30ef358 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Sun, 7 Nov 2021 23:59:06 +0800 Subject: [PATCH] util/tree: fix First/LastNode on empty tree --- util/tree.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/util/tree.c b/util/tree.c index d626aaf..60fceae 100644 --- a/util/tree.c +++ b/util/tree.c @@ -132,13 +132,17 @@ void tree_Delete(tree_Tree *t, tree_Node *node) { tree_Node *tree_FirstNode(tree_Tree *tree) { tree_Node *result = tree->root; - while (result->left) + if (!result) + return 0; + while (result->right) result = result->left; return result; } tree_Node *tree_LastNode(tree_Tree *tree) { tree_Node *result = tree->root; + if (!result) + return 0; while (result->right) result = result->right; return result;