diff mbox

[1/2] Fix wrong memory free on check_is_root

Message ID 54926FB4.6050604@jp.fujitsu.com (mailing list archive)
State New, archived
Headers show

Commit Message

Satoru Takeuchi Dec. 18, 2014, 6:09 a.m. UTC
From: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Date: Thu, 18 Dec 2014 14:35:22 +0900

@tmp is freed even if its allocation fails.

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>

---
 cmds-property.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

Comments

Gui Hecheng Dec. 18, 2014, 7:03 a.m. UTC | #1
On Thu, 2014-12-18 at 15:09 +0900, Satoru Takeuchi wrote:
> From: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
> Date: Thu, 18 Dec 2014 14:35:22 +0900
> 
> @tmp is freed even if its allocation fails.
> 
> Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
> 
> ---
>  cmds-property.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/cmds-property.c b/cmds-property.c
> index a764293..a4bc127 100644
> --- a/cmds-property.c
> +++ b/cmds-property.c
> @@ -140,31 +140,32 @@ static int check_is_root(const char *object)

Hi Satoru,
	" tmp = malloc()
	  if (!tmp) {
		...
		goto out;
	  }
	"
Actually, if the malloc() fails, it returns a NULL, and the free()
handles the NULL internally.
I think there is no special need to deal with this case by the prog
itself.
 
Thanks,
Gui

>  	if (ret < 0) {
>  		fprintf(stderr, "ERROR: get_fsid for %s failed. %s\n", object,
>  				strerror(-ret));
> -		goto out;
> +		goto free_tmp_out;
>  	}
>  
>  	ret = get_fsid(tmp, fsid2, 1);
>  	if (ret == -ENOTTY) {
>  		ret = 0;
> -		goto out;
> +		goto free_tmp_out;
>  	} else if (ret == -ENOTDIR) {
>  		ret = 1;
> -		goto out;
> +		goto free_tmp_out;
>  	} else if (ret < 0) {
>  		fprintf(stderr, "ERROR: get_fsid for %s failed. %s\n", tmp,
>  			strerror(-ret));
> -		goto out;
> +		goto free_tmp_out;
>  	}
>  
>  	if (memcmp(fsid, fsid2, BTRFS_FSID_SIZE)) {
>  		ret = 0;
> -		goto out;
> +		goto free_tmp_out;
>  	}
>  
>  	ret = 1;
>  
> -out:
> +free_tmp_out:
>  	free(tmp);
> +out:
>  	return ret;
>  }
>  


--
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
Satoru Takeuchi Dec. 18, 2014, 7:16 a.m. UTC | #2
On 2014/12/18 16:03, Gui Hecheng wrote:
> On Thu, 2014-12-18 at 15:09 +0900, Satoru Takeuchi wrote:
>> From: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
>> Date: Thu, 18 Dec 2014 14:35:22 +0900
>>
>> @tmp is freed even if its allocation fails.
>>
>> Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
>>
>> ---
>>   cmds-property.c | 13 +++++++------
>>   1 file changed, 7 insertions(+), 6 deletions(-)
>>
>> diff --git a/cmds-property.c b/cmds-property.c
>> index a764293..a4bc127 100644
>> --- a/cmds-property.c
>> +++ b/cmds-property.c
>> @@ -140,31 +140,32 @@ static int check_is_root(const char *object)
>
> Hi Satoru,
> 	" tmp = malloc()
> 	  if (!tmp) {
> 		...
> 		goto out;
> 	  }
> 	"
> Actually, if the malloc() fails, it returns a NULL, and the free()
> handles the NULL internally.
> I think there is no special need to deal with this case by the prog
> itself.

Oh, sorry... my brain would be broken today.

Thanks,
Satoru

>
> Thanks,
> Gui
>
>>   	if (ret < 0) {
>>   		fprintf(stderr, "ERROR: get_fsid for %s failed. %s\n", object,
>>   				strerror(-ret));
>> -		goto out;
>> +		goto free_tmp_out;
>>   	}
>>
>>   	ret = get_fsid(tmp, fsid2, 1);
>>   	if (ret == -ENOTTY) {
>>   		ret = 0;
>> -		goto out;
>> +		goto free_tmp_out;
>>   	} else if (ret == -ENOTDIR) {
>>   		ret = 1;
>> -		goto out;
>> +		goto free_tmp_out;
>>   	} else if (ret < 0) {
>>   		fprintf(stderr, "ERROR: get_fsid for %s failed. %s\n", tmp,
>>   			strerror(-ret));
>> -		goto out;
>> +		goto free_tmp_out;
>>   	}
>>
>>   	if (memcmp(fsid, fsid2, BTRFS_FSID_SIZE)) {
>>   		ret = 0;
>> -		goto out;
>> +		goto free_tmp_out;
>>   	}
>>
>>   	ret = 1;
>>
>> -out:
>> +free_tmp_out:
>>   	free(tmp);
>> +out:
>>   	return ret;
>>   }
>>
>
>
> --
> 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/cmds-property.c b/cmds-property.c
index a764293..a4bc127 100644
--- a/cmds-property.c
+++ b/cmds-property.c
@@ -140,31 +140,32 @@  static int check_is_root(const char *object)
 	if (ret < 0) {
 		fprintf(stderr, "ERROR: get_fsid for %s failed. %s\n", object,
 				strerror(-ret));
-		goto out;
+		goto free_tmp_out;
 	}
 
 	ret = get_fsid(tmp, fsid2, 1);
 	if (ret == -ENOTTY) {
 		ret = 0;
-		goto out;
+		goto free_tmp_out;
 	} else if (ret == -ENOTDIR) {
 		ret = 1;
-		goto out;
+		goto free_tmp_out;
 	} else if (ret < 0) {
 		fprintf(stderr, "ERROR: get_fsid for %s failed. %s\n", tmp,
 			strerror(-ret));
-		goto out;
+		goto free_tmp_out;
 	}
 
 	if (memcmp(fsid, fsid2, BTRFS_FSID_SIZE)) {
 		ret = 0;
-		goto out;
+		goto free_tmp_out;
 	}
 
 	ret = 1;
 
-out:
+free_tmp_out:
 	free(tmp);
+out:
 	return ret;
 }