Skip to contentSkip to conversation

fs: btrfs: Add an assertion to warn incorrct case in insert_inline_extent()

PATCH In review Jia-Ju Bai 3 participants 3 messages Jul 27, 2019 – Jul 30, 2019 Btrfs file system

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/1 fs: btrfs: Add an assertion to warn incorrct case in insert_inline_extent()

In insert_inline_extent(), the case that compressed_size > 0 and compressed_pages = NULL cannot occur, otherwise a null-pointer dereference may occur on line 215:

     cpage = compressed_pages[i];

To warn this incorrect case, an assertion is added. Thank Qu Wenruo and David Sterba for good advice.

---
 fs/btrfs/inode.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/inode.c +3 -0
index 1af069a9a0c7..21d6e2dcc25f 100644
@@ -178,6 +178,9 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,
 	size_t cur_size = size;
 	unsigned long offset;
 
+	ASSERT((compressed_size > 0 && compressed_pages) ||
+			(compressed_size == 0 && !compressed_pages))
+
 	if (compressed_size && compressed_pages)
 		cur_size = compressed_size;
 
Signature · 1 line
2.17.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Jia-Ju Bai

In insert_inline_extent(), the case that compressed_size > 0 and compressed_pages = NULL cannot occur, otherwise a null-pointer dereference may occur on line 215:

     cpage = compressed_pages[i];

To warn this incorrect case, an assertion is added. Thank Qu Wenruo and David Sterba for good advice.

Thanks,
Qu

Quoted 19 lines
---
 fs/btrfs/inode.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/inode.c +3 -0
index 1af069a9a0c7..21d6e2dcc25f 100644
@@ -178,6 +178,9 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,
 	size_t cur_size = size;
 	unsigned long offset;
 
+	ASSERT((compressed_size > 0 && compressed_pages) ||
+			(compressed_size == 0 && !compressed_pages))
+
 	if (compressed_size && compressed_pages)
 		cur_size = compressed_size;
 
#2 lore raw
In reply to Jia-Ju Bai
Quoted 18 lines · Jia-Ju Bai

In insert_inline_extent(), the case that compressed_size > 0 and compressed_pages = NULL cannot occur, otherwise a null-pointer dereference may occur on line 215:

     cpage = compressed_pages[i];

To warn this incorrect case, an assertion is added. Thank Qu Wenruo and David Sterba for good advice.

---
 fs/btrfs/inode.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/inode.c +0 -0
index 1af069a9a0c7..21d6e2dcc25f 100644

fs/btrfs/inode.c static int insert_inline_extent(struct btrfs_trans_handle *trans, · line 182

Quoted 5 lines
fs/btrfs/inode.c +2 -0
@@ -178,6 +178,9 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,
 	size_t cur_size = size;
 	unsigned long offset;
 
+	ASSERT((compressed_size > 0 && compressed_pages) ||
+			(compressed_size == 0 && !compressed_pages))

Thanks. I expect that the static checking tools can be instructed to understand that the condition has been checked and is not missing in the code below. ASSERT is conditinally a BUG() wrapper, otherwise a no-op.

Btw, it's also good to check that the code compiles, the statement is missing semicolon.