Skip to contentSkip to conversation

btrfs: clean up NULL checks in qgroup_unreserve_range()

PATCH In review Dan Carpenter 3 participants 3 messages Oct 23, 2020 – Oct 29, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: clean up NULL checks in qgroup_unreserve_range()

Smatch complains that this code dereferences "entry" before checking whether it's NULL on the next line. Fortunately, rb_entry() will never return NULL so it doesn't cause a problem. We can clean up the NULL checking a bit to silence the warning and make the code more clear.

---
My patch does not change run-time, but it's possible that the original code was buggy and I missed it.

 fs/btrfs/qgroup.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
fs/btrfs/qgroup.c +4 -8
index 580899bdb991..a7ae2f18f486 100644
@@ -3417,24 +3417,20 @@ static int qgroup_unreserve_range(struct btrfs_inode *inode,
 {
 	struct rb_node *node;
 	struct rb_node *next;
-	struct ulist_node *entry = NULL;
+	struct ulist_node *entry;
 	int ret = 0;
 
 	node = reserved->range_changed.root.rb_node;
+	if (!node)
+		return 0;
 	while (node) {
 		entry = rb_entry(node, struct ulist_node, rb_node);
 		if (entry->val < start)
 			node = node->rb_right;
-		else if (entry)
-			node = node->rb_left;
 		else
-			break;
+			node = node->rb_left;
 	}
 
-	/* Empty changeset */
-	if (!entry)
-		return 0;
-
 	if (entry->val > start && rb_prev(&entry->rb_node))
 		entry = rb_entry(rb_prev(&entry->rb_node), struct ulist_node,
 				 rb_node);
Signature · 1 line
2.28.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 28 lines · Dan Carpenter

Smatch complains that this code dereferences "entry" before checking whether it's NULL on the next line. Fortunately, rb_entry() will never return NULL so it doesn't cause a problem. We can clean up the NULL checking a bit to silence the warning and make the code more clear.

---
My patch does not change run-time, but it's possible that the original code was buggy and I missed it.

 fs/btrfs/qgroup.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
fs/btrfs/qgroup.c +3 -1
index 580899bdb991..a7ae2f18f486 100644
@@ -3417,24 +3417,20 @@ static int qgroup_unreserve_range(struct btrfs_inode *inode,
 {
 	struct rb_node *node;
 	struct rb_node *next;
-	struct ulist_node *entry = NULL;
+	struct ulist_node *entry;
 	int ret = 0;
 
 	node = reserved->range_changed.root.rb_node;
+	if (!node)
+		return 0;

fs/btrfs/qgroup.c static int qgroup_unreserve_range(struct btrfs_inode *inode, · line 3432

Quoted 10 lines
fs/btrfs/qgroup.c +1 -3
 	while (node) {
 		entry = rb_entry(node, struct ulist_node, rb_node);
 		if (entry->val < start)
 			node = node->rb_right;
-		else if (entry)
-			node = node->rb_left;
 		else
-			break;
+			node = node->rb_left;
 	}

Indeed, the new result does the same work, by going to the nearest node of @start.

New code looks like:

while (node) {
	if (entry->val < start)
		node = node->rb_right;
	else
		node = node->rb_left;
}

is good enough and removes the dead break branch.

Looks good to me.

Thanks,
Qu

Quoted 9 lines
fs/btrfs/qgroup.c +0 -4
-	/* Empty changeset */
-	if (!entry)
-		return 0;
-
 	if (entry->val > start && rb_prev(&entry->rb_node))
 		entry = rb_entry(rb_prev(&entry->rb_node), struct ulist_node,
 				 rb_node);
 
#2 lore raw
In reply to Dan Carpenter
Quoted 6 lines · Dan Carpenter

Smatch complains that this code dereferences "entry" before checking whether it's NULL on the next line. Fortunately, rb_entry() will never return NULL so it doesn't cause a problem. We can clean up the NULL checking a bit to silence the warning and make the code more clear.

Added to misc-next, thanks.