From: Jyun-An Chen <jun930436@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, Jyun-An Chen <jun930436@gmail.com>
Subject: [PATCH 1/2] lib/btree: fix btree_remove() of a key 0 that is not in the tree
Date: Sun, 6 Dec 2026 07:07:22 +0800 [thread overview]
Message-ID: <20261205230723.96179-2-jun930436@gmail.com> (raw)
In-Reply-To: <20261205230723.96179-1-jun930436@gmail.com>
Unused slots in a btree node hold key 0 and a NULL value. When
btree_remove() is called for key 0 and the tree does not contain it,
getpos() stops at the first unused slot of the leaf (pos == fill), and
keycmp() reports a match. btree_remove_level() then removes that slot:
nothing is shifted, and clearpair(geo, node, fill - 1) wipes the
smallest real entry of the leaf, which is dropped from the tree without
its value being returned to the caller. If the leaf is the empty root
node left behind after the last entry was removed, fill is 0 and
clearpair() writes in front of the node. If the leaf is full, pos is
geo->no_pairs and keycmp() reads a value slot as if it were a key.
For example, with keys 1..5 in a btree32, btree_remove() of key 0
returns NULL, and afterwards btree_lookup() of key 1 returns NULL too.
Treat pos == fill as a miss.
Fixes: 5db53f3e80de ("[LogFS] add new flash file system")
Signed-off-by: Jyun-An Chen <jun930436@gmail.com>
---
I used an AI coding assistant (Claude Code) to write simple tests
to check that this change works.
lib/btree.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/btree.c b/lib/btree.c
index 9c80c0c7bba8..3c2560173c62 100644
--- a/lib/btree.c
+++ b/lib/btree.c
@@ -600,7 +600,7 @@ static void *btree_remove_level(struct btree_head *head, struct btree_geo *geo,
node = find_level(head, geo, key, level);
pos = getpos(geo, node, key);
fill = getfill(geo, node, pos);
- if ((level == 1) && (keycmp(geo, node, pos, key) != 0))
+ if (level == 1 && (pos == fill || keycmp(geo, node, pos, key) != 0))
return NULL;
ret = bval(geo, node, pos);
next prev parent reply other threads:[~2026-12-05 23:07 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 23:07 [PATCH 0/2] lib/btree: fix two bugs with key 0 Jyun-An Chen
2026-12-05 23:07 ` Jyun-An Chen [this message]
2026-12-05 23:07 ` [PATCH 2/2] lib/btree: stop btree_get_prev() from wrapping around below " Jyun-An Chen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261205230723.96179-2-jun930436@gmail.com \
--to=jun930436@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.