diff mbox

btrfs-progs: add malloc check in transaction.h/btrfs_start_transaction

Message ID 502CA7E6.8020004@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Wang Sheng-Hui Aug. 16, 2012, 7:57 a.m. UTC
For malloc may fail, we should check it before assign
values to the fields of struct btrfs_trans_handle *h.

Signed-off-by: Wang Sheng-Hui <shhuiw@gmail.com>
---
 transaction.h |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)

Comments

Arne Jansen Aug. 16, 2012, 8:03 a.m. UTC | #1
On 16.08.2012 09:57, Wang Sheng-Hui wrote:
> For malloc may fail, we should check it before assign
> values to the fields of struct btrfs_trans_handle *h.
> 
> Signed-off-by: Wang Sheng-Hui <shhuiw@gmail.com>
> ---
>  transaction.h |    5 +++++
>  1 files changed, 5 insertions(+), 0 deletions(-)
> 
> diff --git a/transaction.h b/transaction.h
> index a1070e0..d4e42a1 100644
> --- a/transaction.h
> +++ b/transaction.h
> @@ -32,7 +32,12 @@ static inline struct btrfs_trans_handle *
>  btrfs_start_transaction(struct btrfs_root *root, int num_blocks)
>  {
>  	struct btrfs_fs_info *fs_info = root->fs_info;
> +
>  	struct btrfs_trans_handle *h = malloc(sizeof(*h));
> +	if (!h) {
> +		BUG();
> +		return NULL;
> +	}

a more simple way would be to write
BUG_ON(!h);

>  
>  	BUG_ON(root->commit_root);
>  	BUG_ON(fs_info->running_transaction);

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
jeff.liu Aug. 16, 2012, 8:55 a.m. UTC | #2
On 08/16/12 16:03, Arne Jansen wrote:
> On 16.08.2012 09:57, Wang Sheng-Hui wrote:
>> For malloc may fail, we should check it before assign
>> values to the fields of struct btrfs_trans_handle *h.
>>
>> Signed-off-by: Wang Sheng-Hui <shhuiw@gmail.com>
>> ---
>>  transaction.h |    5 +++++
>>  1 files changed, 5 insertions(+), 0 deletions(-)
>>
>> diff --git a/transaction.h b/transaction.h
>> index a1070e0..d4e42a1 100644
>> --- a/transaction.h
>> +++ b/transaction.h
>> @@ -32,7 +32,12 @@ static inline struct btrfs_trans_handle *
>>  btrfs_start_transaction(struct btrfs_root *root, int num_blocks)
>>  {
>>  	struct btrfs_fs_info *fs_info = root->fs_info;
>> +
Why move down a blank line?
>>  	struct btrfs_trans_handle *h = malloc(sizeof(*h));
Skip a line here would looks a bit neat.
>> +	if (!h) {
>> +		BUG();
>> +		return NULL;
>> +	}
> a more simple way would be to write
> BUG_ON(!h);
>
>>  
>>  	BUG_ON(root->commit_root);
>>  	BUG_ON(fs_info->running_transaction);
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/transaction.h b/transaction.h
index a1070e0..d4e42a1 100644
--- a/transaction.h
+++ b/transaction.h
@@ -32,7 +32,12 @@  static inline struct btrfs_trans_handle *
 btrfs_start_transaction(struct btrfs_root *root, int num_blocks)
 {
 	struct btrfs_fs_info *fs_info = root->fs_info;
+
 	struct btrfs_trans_handle *h = malloc(sizeof(*h));
+	if (!h) {
+		BUG();
+		return NULL;
+	}
 
 	BUG_ON(root->commit_root);
 	BUG_ON(fs_info->running_transaction);