diff mbox series

[2/2] hugetlbfs: Improve exception handling in hugetlbfs_fill_super()

Message ID b109b7dc-3972-4b2e-ae4c-89bf8eecf8f2@web.de (mailing list archive)
State New
Headers show
Series hugetlbfs: Adjustments for two function implementations | expand

Commit Message

Markus Elfring Dec. 29, 2023, 11:40 a.m. UTC
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Fri, 29 Dec 2023 11:46:32 +0100

The kfree() function was called in one case by
the hugetlbfs_fill_super() function during error handling
even if the passed variable contained a null pointer.
This issue was detected by using the Coccinelle software.

Thus adjust jump targets.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 fs/hugetlbfs/inode.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

--
2.43.0

Comments

Matthew Wilcox Dec. 29, 2023, 2:52 p.m. UTC | #1
On Fri, Dec 29, 2023 at 12:40:12PM +0100, Markus Elfring wrote:
> diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
> index 24401a5046dd..5687ec574dc4 100644
> --- a/fs/hugetlbfs/inode.c
> +++ b/fs/hugetlbfs/inode.c
> @@ -1483,7 +1483,7 @@ hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
>  						     ctx->max_hpages,
>  						     ctx->min_hpages);
>  		if (!sbinfo->spool)
> -			goto out_free;
> +			goto free_sbinfo;
>  	}
>  	sb->s_maxbytes = MAX_LFS_FILESIZE;
>  	sb->s_blocksize = huge_page_size(ctx->hstate);
> @@ -1499,10 +1499,12 @@ hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
>  	sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
>  	sb->s_root = d_make_root(hugetlbfs_get_root(sb, ctx));
>  	if (!sb->s_root)
> -		goto out_free;
> +		goto free_spool;
>  	return 0;
> -out_free:
> +
> +free_spool:
>  	kfree(sbinfo->spool);
> +free_sbinfo:
>  	kfree(sbinfo);
>  	return -ENOMEM;
>  }

This is more complex.  NACK.
Markus Elfring Dec. 30, 2023, 6:45 a.m. UTC | #2
>> +++ b/fs/hugetlbfs/inode.c
>> @@ -1483,7 +1483,7 @@ hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
>>  						     ctx->max_hpages,
>>  						     ctx->min_hpages);
>>  		if (!sbinfo->spool)
>> -			goto out_free;
>> +			goto free_sbinfo;
>>  	}
>>  	sb->s_maxbytes = MAX_LFS_FILESIZE;
>>  	sb->s_blocksize = huge_page_size(ctx->hstate);
>> @@ -1499,10 +1499,12 @@ hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
>>  	sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
>>  	sb->s_root = d_make_root(hugetlbfs_get_root(sb, ctx));
>>  	if (!sb->s_root)
>> -		goto out_free;
>> +		goto free_spool;
>>  	return 0;
>> -out_free:
>> +
>> +free_spool:
>>  	kfree(sbinfo->spool);
>> +free_sbinfo:
>>  	kfree(sbinfo);
>>  	return -ENOMEM;
>>  }
>
> This is more complex.  NACK.

I am curious how coding style preferences will evolve further.

See also:
https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources

Regards,
Markus
diff mbox series

Patch

diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 24401a5046dd..5687ec574dc4 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -1483,7 +1483,7 @@  hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
 						     ctx->max_hpages,
 						     ctx->min_hpages);
 		if (!sbinfo->spool)
-			goto out_free;
+			goto free_sbinfo;
 	}
 	sb->s_maxbytes = MAX_LFS_FILESIZE;
 	sb->s_blocksize = huge_page_size(ctx->hstate);
@@ -1499,10 +1499,12 @@  hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
 	sb->s_root = d_make_root(hugetlbfs_get_root(sb, ctx));
 	if (!sb->s_root)
-		goto out_free;
+		goto free_spool;
 	return 0;
-out_free:
+
+free_spool:
 	kfree(sbinfo->spool);
+free_sbinfo:
 	kfree(sbinfo);
 	return -ENOMEM;
 }