diff mbox series

[f2fs-dev] fsck.f2fs: fix potential NULL dereference

Message ID 20230531065142.365199-1-korotkov.maxim.s@gmail.com (mailing list archive)
State Superseded
Headers show
Series [f2fs-dev] fsck.f2fs: fix potential NULL dereference | expand

Commit Message

Maxim Korotkov May 31, 2023, 6:51 a.m. UTC
The input pointer "parent" was used unsafely
before checking against NULL

Found by RASU JSC with Svace static analyzer
Fixes: 603f8f9d3(sload.f2fs: support loading files into partition directly)
Signed-off-by: Maxim Korotkov <maskorotkov@rasu.ru>
---
 fsck/dir.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

Comments

Chao Yu June 2, 2023, 9:38 a.m. UTC | #1
On 2023/5/31 14:51, Maxim Korotkov wrote:
> The input pointer "parent" was used unsafely
> before checking against NULL
> 
> Found by RASU JSC with Svace static analyzer
> Fixes: 603f8f9d3(sload.f2fs: support loading files into partition directly)
> Signed-off-by: Maxim Korotkov <maskorotkov@rasu.ru>
> ---
>   fsck/dir.c | 14 +++++++++-----
>   1 file changed, 9 insertions(+), 5 deletions(-)
> 
> diff --git a/fsck/dir.c b/fsck/dir.c
> index 4a3eb6e..925fe14 100644
> --- a/fsck/dir.c
> +++ b/fsck/dir.c
> @@ -225,19 +225,23 @@ int f2fs_add_link(struct f2fs_sb_info *sbi, struct f2fs_node *parent,
>   	int level = 0, current_depth, bit_pos;
>   	int nbucket, nblock, bidx, block;
>   	int slots = GET_DENTRY_SLOTS(name_len);
> -	f2fs_hash_t dentry_hash = f2fs_dentry_hash(get_encoding(sbi),
> -						IS_CASEFOLDED(&parent->i),
> -						name, name_len);
> +	f2fs_hash_t dentry_hash;
>   	struct f2fs_dentry_block *dentry_blk;
>   	struct f2fs_dentry_ptr d;
>   	struct dnode_of_data dn;
> -	nid_t pino = le32_to_cpu(parent->footer.ino);
> -	unsigned int dir_level = parent->i.i_dir_level;
> +	nid_t pino;
> +	unsigned int dir_level;
>   	int ret;
>   
>   	if (parent == NULL)
>   		return -EINVAL;
>   
> +	dentry_hash = f2fs_dentry_hash(get_encoding(sbi),
> +						IS_CASEFOLDED(&parent->i),
> +						name, name_len);	

Unneeded indent after ';', otherwise it looks good to me.

Thanks,

> +	pino = le32_to_cpu(parent->footer.ino);
> +	dir_level = parent->i.i_dir_level;
> +
>   	if (!pino) {
>   		ERR_MSG("Wrong parent ino:%d \n", pino);
>   		return -EINVAL;
diff mbox series

Patch

diff --git a/fsck/dir.c b/fsck/dir.c
index 4a3eb6e..925fe14 100644
--- a/fsck/dir.c
+++ b/fsck/dir.c
@@ -225,19 +225,23 @@  int f2fs_add_link(struct f2fs_sb_info *sbi, struct f2fs_node *parent,
 	int level = 0, current_depth, bit_pos;
 	int nbucket, nblock, bidx, block;
 	int slots = GET_DENTRY_SLOTS(name_len);
-	f2fs_hash_t dentry_hash = f2fs_dentry_hash(get_encoding(sbi),
-						IS_CASEFOLDED(&parent->i),
-						name, name_len);
+	f2fs_hash_t dentry_hash;
 	struct f2fs_dentry_block *dentry_blk;
 	struct f2fs_dentry_ptr d;
 	struct dnode_of_data dn;
-	nid_t pino = le32_to_cpu(parent->footer.ino);
-	unsigned int dir_level = parent->i.i_dir_level;
+	nid_t pino;
+	unsigned int dir_level;
 	int ret;
 
 	if (parent == NULL)
 		return -EINVAL;
 
+	dentry_hash = f2fs_dentry_hash(get_encoding(sbi),
+						IS_CASEFOLDED(&parent->i),
+						name, name_len);	
+	pino = le32_to_cpu(parent->footer.ino);
+	dir_level = parent->i.i_dir_level;
+
 	if (!pino) {
 		ERR_MSG("Wrong parent ino:%d \n", pino);
 		return -EINVAL;