summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2019-10-01 06:37:24 -0700
committerKaz Kylheku <kaz@kylheku.com>2019-10-01 06:37:24 -0700
commita9fea6ce0bc4f1cfe52dd3f80432b2bef297d4ef (patch)
treedb7cd4112a5431e3a543bfa2e090c65b67a644b0
parent26e5a58c5bed05cc928d7989f991b7655193c82c (diff)
downloadtxr-a9fea6ce0bc4f1cfe52dd3f80432b2bef297d4ef.tar.gz
txr-a9fea6ce0bc4f1cfe52dd3f80432b2bef297d4ef.tar.bz2
txr-a9fea6ce0bc4f1cfe52dd3f80432b2bef297d4ef.zip
tree: crash when root is to be replaced.
* tree.c (tr_insert): When the inserted key matches the root node, then there is nothing in ti->path and ti->depth is zero. We are accessing ti->path[-1] to get a root node. We must test for zero depth and install the new node as the tree root in that case.
-rw-r--r--tree.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/tree.c b/tree.c
index 45132648..b7e528eb 100644
--- a/tree.c
+++ b/tree.c
@@ -265,11 +265,16 @@ static void tr_insert(struct tree *tr, struct tree_iter *ti,
val parent = ti->path[ti->depth - 1];
node->tn.left = subtree->tn.left;
node->tn.right = subtree->tn.right;
+ if (ti->depth > 0) {
+ val parent = ti->path[ti->depth - 1];
- if (parent->tn.left == subtree)
- parent->tn.left = node;
- else
- parent->tn.right = node;
+ if (parent->tn.left == subtree)
+ parent->tn.left = node;
+ else
+ parent->tn.right = node;
+ } else {
+ tr->root = node;
+ }
} else {
if (subtree->tn.right) {
ti->path[ti->depth++] = subtree;