linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs: btrfs: Add an assertion to warn incorrct case in insert_inline_extent()
@ 2019-07-27  8:51 Jia-Ju Bai
  2019-07-27  9:07 ` Qu Wenruo
  2019-07-30 16:33 ` David Sterba
  0 siblings, 2 replies; 3+ messages in thread
From: Jia-Ju Bai @ 2019-07-27  8:51 UTC (permalink / raw)
  To: clm, josef, dsterba, quwenruo.btrfs; +Cc: linux-btrfs, linux-kernel, 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.

Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
---
 fs/btrfs/inode.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 1af069a9a0c7..21d6e2dcc25f 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -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.17.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] fs: btrfs: Add an assertion to warn incorrct case in insert_inline_extent()
  2019-07-27  8:51 [PATCH] fs: btrfs: Add an assertion to warn incorrct case in insert_inline_extent() Jia-Ju Bai
@ 2019-07-27  9:07 ` Qu Wenruo
  2019-07-30 16:33 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: Qu Wenruo @ 2019-07-27  9:07 UTC (permalink / raw)
  To: Jia-Ju Bai, clm, josef, dsterba; +Cc: linux-btrfs, linux-kernel


[-- Attachment #1.1: Type: text/plain, Size: 1064 bytes --]



On 2019/7/27 下午4:51, Jia-Ju Bai wrote:
> 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.
> 
> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu

> ---
>  fs/btrfs/inode.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 1af069a9a0c7..21d6e2dcc25f 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -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;
>  
> 


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] fs: btrfs: Add an assertion to warn incorrct case in insert_inline_extent()
  2019-07-27  8:51 [PATCH] fs: btrfs: Add an assertion to warn incorrct case in insert_inline_extent() Jia-Ju Bai
  2019-07-27  9:07 ` Qu Wenruo
@ 2019-07-30 16:33 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2019-07-30 16:33 UTC (permalink / raw)
  To: Jia-Ju Bai; +Cc: clm, josef, dsterba, quwenruo.btrfs, linux-btrfs, linux-kernel

On Sat, Jul 27, 2019 at 04:51:13PM +0800, Jia-Ju Bai wrote:
> 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.
> 
> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> ---
>  fs/btrfs/inode.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 1af069a9a0c7..21d6e2dcc25f 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-07-30 16:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-27  8:51 [PATCH] fs: btrfs: Add an assertion to warn incorrct case in insert_inline_extent() Jia-Ju Bai
2019-07-27  9:07 ` Qu Wenruo
2019-07-30 16:33 ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).