Skip to contentSkip to conversation

btrfs: assume fs_info is non-NULL and drop ternary check

RFC In review Antonio Quartulli 2 participants 3 messages Sep 26, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

RFC 1/1 btrfs: assume fs_info is non-NULL and drop ternary check

When assigning max_bytes, fs_info is checked for being non-NULL and a value is assigned accordingly.
However, a few lines below we do dereference fs_info multiple times without checking whether it is non-NULL anymore.

This pattern has triggered Coverity Scan which thinks that we may hit a NULL-ptr-dereference.

If we know for sure that fs_info cannot be NULL (and this seems to be the case since various instructions assume so), we should drop the ternary check when assigning max_bytes in order to avoid confusing the casual reader or static analyzers.

Fixes f7b12a62f008a ("btrfs: replace BTRFS_MAX_EXTENT_SIZE with fs_info->max_extent_size")

Address-Coverity-ID: 1659628 ("Null pointer dereferences (FORWARD_NULL)")

---
 fs/btrfs/extent_io.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/extent_io.c +1 -2
index c123a3ef154ae..dfda8f6da1940 100644
@@ -374,8 +374,7 @@ noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
 	const u64 orig_start = *start;
 	const u64 orig_end = *end;
-	/* The sanity tests may not set a valid fs_info. */
-	u64 max_bytes = fs_info ? fs_info->max_extent_size : BTRFS_MAX_EXTENT_SIZE;
+	u64 max_bytes = fs_info->max_extent_size;
 	u64 delalloc_start;
 	u64 delalloc_end;
 	bool found;
Signature · 1 line
2.49.1
#1 lore raw
Quoted 8 lines · Antonio Quartulli

When assigning max_bytes, fs_info is checked for being non-NULL and a value is assigned accordingly.
However, a few lines below we do dereference fs_info multiple times without checking whether it is non-NULL anymore.

This pattern has triggered Coverity Scan which thinks that we may hit a NULL-ptr-dereference.

Quoted 8 lines

If we know for sure that fs_info cannot be NULL (and this seems to be the case since various instructions assume so), we should drop the ternary check when assigning max_bytes in order to avoid confusing the casual reader or static analyzers.

Fixes f7b12a62f008a ("btrfs: replace BTRFS_MAX_EXTENT_SIZE with fs_info->max_extent_size")

Address-Coverity-ID: 1659628 ("Null pointer dereferences (FORWARD_NULL)")

Thanks, the fix for that was sent yesterday and is now in for-next.

https://lore.kernel.org/linux-btrfs/686d2506851c2df8cb9ceab5241b15bb01737ebb.1758793968.git.wqu@suse.com/