public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP]  [PATCH]: Fix memory leak by freeing buf
@ 2015-05-28  4:44 Manjeet Pawar
  2015-06-01 14:49 ` Cyril Hrubis
  0 siblings, 1 reply; 5+ messages in thread
From: Manjeet Pawar @ 2015-05-28  4:44 UTC (permalink / raw)
  To: ltp-list@lists.sourceforge.net; +Cc: ajeet.y@samsung.com, Yogesh Narayan Gaur

From: Manjeet Pawar <manjeet.p@samsung.com>
Subject: [PATCH] pan/ltp-pan.c: free buf before function return to avoid memory leak

This patch avoid memory leak by freeing the buf before the slurp() function return

Signed-off-by: Yogesh Gaur <yn.gaur@samsung.com>
Signed-off-by: Manjeet Pawar <manjeet.p@samsung.com>
---
 pan/ltp-pan.c |    1 +
 1 file changed, 1 insertion(+)

diff --git a/pan/ltp-pan.c b/pan/ltp-pan.c
index 4c1e9de..905ff89 100644
--- a/pan/ltp-pan.c
+++ b/pan/ltp-pan.c
@@ -1279,6 +1279,7 @@ static char *slurp(char *file)
 	if (read(fd, buf, sbuf.st_size) != sbuf.st_size) {
 		fprintf(stderr, "pan(%s): slurp failed.  errno:%d  %s\n",
 			panname, errno, strerror(errno));
+		free(buf);
 		return NULL;
 	}
 	buf[sbuf.st_size] = '\0';
-- 
1.7.9.5


------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH]: Fix memory leak by freeing buf
  2015-05-28  4:44 Manjeet Pawar
@ 2015-06-01 14:49 ` Cyril Hrubis
  0 siblings, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2015-06-01 14:49 UTC (permalink / raw)
  To: Manjeet Pawar
  Cc: ajeet.y@samsung.com, ltp-list@lists.sourceforge.net,
	Yogesh Narayan Gaur

Hi!
Looking at the code, ltp-pan exits if this function fails, so the danger
of memory leak was not real here.

But it's better to fix the function, pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP]  [PATCH]: Fix memory leak by freeing buf
@ 2015-06-19 11:25 Manjeet Pawar
  2015-06-19 12:09 ` Alexey Kodanev
  0 siblings, 1 reply; 5+ messages in thread
From: Manjeet Pawar @ 2015-06-19 11:25 UTC (permalink / raw)
  To: ltp-list@lists.sourceforge.net; +Cc: ajeet.y@samsung.com, AKHILESH KUMAR

From: Manjeet Pawar <manjeet.p@samsung.com>
Date: Fri, 19 Jun 2015 16:45:54 +0530
Subject: [PATCH] testcases/open_posix_testsuite/functional/semaphores: Fix memory leak by freeing buf

This patch fix memory leak by freeing memory leak before function return

Signed-off-by: Manjeet Pawar <manjeet.p@samsung.com>
Signed-off-by: Akhilesh Kumar <akhilesh.k@samsung.com>
---
 .../functional/semaphores/sem_conpro.c             |    5 +++++
 1 file changed, 5 insertions(+)

diff --git a/testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c b/testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c
index f5f1abf..f3a4cfb 100644
--- a/testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c
+++ b/testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c
@@ -106,19 +106,23 @@ int main(int argc, char *argv[])
 
 #ifndef  _POSIX_SEMAPHORES
 	printf("_POSIX_SEMAPHORES is not defined \n");
+	free(buf);
 	return PTS_UNRESOLVED;
 #endif
 	if (-1 == sem_init(&(buf->occupied), shared, occupied_value)) {
 		perror("sem_init didn't return success \n");
 		printf("hello \n");
+		free(buf);
 		return PTS_UNRESOLVED;
 	}
 	if (-1 == sem_init(&buf->empty, shared, empty_value)) {
 		perror("sem_init didn't return success \n");
+		free(buf);
 		return PTS_UNRESOLVED;
 	}
 	if (-1 == sem_init(&buf->lock, shared, lock_value)) {
 		perror("sem_init didn't return success \n");
+		free(buf);
 		return PTS_UNRESOLVED;
 	}
 	in = out = 0;
@@ -129,5 +133,6 @@ int main(int argc, char *argv[])
 	pthread_join(pro, NULL);
 	sem_destroy(&buf->occupied);
 	sem_destroy(&buf->empty);
+	free(buf);
 	return PTS_PASS;
 }
-- 
1.7.9.5

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH]: Fix memory leak by freeing buf
  2015-06-19 11:25 [LTP] [PATCH]: Fix memory leak by freeing buf Manjeet Pawar
@ 2015-06-19 12:09 ` Alexey Kodanev
  2015-06-19 12:20   ` Alexey Kodanev
  0 siblings, 1 reply; 5+ messages in thread
From: Alexey Kodanev @ 2015-06-19 12:09 UTC (permalink / raw)
  To: manjeet.p, ltp-list@lists.sourceforge.net
  Cc: ajeet.y@samsung.com, AKHILESH KUMAR

Hi!
On 06/19/2015 02:25 PM, Manjeet Pawar wrote:
> From: Manjeet Pawar <manjeet.p@samsung.com>
> Date: Fri, 19 Jun 2015 16:45:54 +0530
> Subject: [PATCH] testcases/open_posix_testsuite/functional/semaphores: Fix memory leak by freeing buf
>
> This patch fix memory leak by freeing memory leak before function return
>
> Signed-off-by: Manjeet Pawar <manjeet.p@samsung.com>
> Signed-off-by: Akhilesh Kumar <akhilesh.k@samsung.com>
> ---
>   .../functional/semaphores/sem_conpro.c             |    5 +++++
>   1 file changed, 5 insertions(+)
>
> diff --git a/testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c b/testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c
> index f5f1abf..f3a4cfb 100644
> --- a/testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c
> +++ b/testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c
> @@ -106,19 +106,23 @@ int main(int argc, char *argv[])
>   
>   #ifndef  _POSIX_SEMAPHORES
>   	printf("_POSIX_SEMAPHORES is not defined \n");
> +	free(buf);
>   	return PTS_UNRESOLVED;
>   #endif
>   	if (-1 == sem_init(&(buf->occupied), shared, occupied_value)) {
>   		perror("sem_init didn't return success \n");
>   		printf("hello \n");
> +		free(buf);
>   		return PTS_UNRESOLVED;
>   	}
>   	if (-1 == sem_init(&buf->empty, shared, empty_value)) {
>   		perror("sem_init didn't return success \n");
> +		free(buf);
>   		return PTS_UNRESOLVED;
>   	}
>   	if (-1 == sem_init(&buf->lock, shared, lock_value)) {
>   		perror("sem_init didn't return success \n");
> +		free(buf);
>   		return PTS_UNRESOLVED;
>   	}
>   	in = out = 0;
> @@ -129,5 +133,6 @@ int main(int argc, char *argv[])
>   	pthread_join(pro, NULL);
>   	sem_destroy(&buf->occupied);
>   	sem_destroy(&buf->empty);
> +	free(buf);
>   	return PTS_PASS;
>   }

The program terminating after free() so it will be freed in any case, I 
don't see memory leak here.

I think the better change would be to check the return value from 
malloc() (e.g. using SAFE_MALLOC) or allocate 'buf' on the stack.

Thanks,
Alexey


------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH]: Fix memory leak by freeing buf
  2015-06-19 12:09 ` Alexey Kodanev
@ 2015-06-19 12:20   ` Alexey Kodanev
  0 siblings, 0 replies; 5+ messages in thread
From: Alexey Kodanev @ 2015-06-19 12:20 UTC (permalink / raw)
  To: manjeet.p, ltp-list@lists.sourceforge.net
  Cc: ajeet.y@samsung.com, AKHILESH KUMAR


On 06/19/2015 03:09 PM, Alexey Kodanev wrote:
> Hi!
> On 06/19/2015 02:25 PM, Manjeet Pawar wrote:
>> From: Manjeet Pawar <manjeet.p@samsung.com>
>> Date: Fri, 19 Jun 2015 16:45:54 +0530
>> Subject: [PATCH] testcases/open_posix_testsuite/functional/semaphores: Fix memory leak by freeing buf
>>
>> This patch fix memory leak by freeing memory leak before function return
>>
>> Signed-off-by: Manjeet Pawar <manjeet.p@samsung.com>
>> Signed-off-by: Akhilesh Kumar <akhilesh.k@samsung.com>
>> ---
>>    .../functional/semaphores/sem_conpro.c             |    5 +++++
>>    1 file changed, 5 insertions(+)
>>
>> diff --git a/testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c b/testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c
>> index f5f1abf..f3a4cfb 100644
>> --- a/testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c
>> +++ b/testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c
>> @@ -106,19 +106,23 @@ int main(int argc, char *argv[])
>>    
>>    #ifndef  _POSIX_SEMAPHORES
>>    	printf("_POSIX_SEMAPHORES is not defined \n");
>> +	free(buf);
>>    	return PTS_UNRESOLVED;
>>    #endif
>>    	if (-1 == sem_init(&(buf->occupied), shared, occupied_value)) {
>>    		perror("sem_init didn't return success \n");
>>    		printf("hello \n");

Also, do you know why it prints "hello"? It should be removed I guess.

Thanks,
Alexey

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

end of thread, other threads:[~2015-06-19 12:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-19 11:25 [LTP] [PATCH]: Fix memory leak by freeing buf Manjeet Pawar
2015-06-19 12:09 ` Alexey Kodanev
2015-06-19 12:20   ` Alexey Kodanev
  -- strict thread matches above, loose matches on Subject: below --
2015-05-28  4:44 Manjeet Pawar
2015-06-01 14:49 ` Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox