Skip to contentSkip to conversation

: Fix memory leak by freeing buf

PATCH In review Manjeet Pawar 2 participants 3 messages Jun 19, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 : Fix memory leak by freeing buf

From: Manjeet Pawar <[email protected]>
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

---
 .../functional/semaphores/sem_conpro.c             |    5 +++++
 1 file changed, 5 insertions(+)
testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c +5 -0
index f5f1abf..f3a4cfb 100644
@@ -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;
 }
Signature · 7 lines
1.7.9.5

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#1 lore raw

Hi!

Quoted 41 lines · Manjeet Pawar

From: Manjeet Pawar <[email protected]>
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

---
  .../functional/semaphores/sem_conpro.c             |    5 +++++
  1 file changed, 5 insertions(+)
testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c +4 -0
index f5f1abf..f3a4cfb 100644
@@ -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;

testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c int main(int argc, char *argv[]) · line 138

Quoted 6 lines
testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c +1 -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
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#2 lore raw
Quoted 20 lines · Alexey Kodanev

Hi!

Quoted 18 lines · Manjeet Pawar

From: Manjeet Pawar <[email protected]>
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

---
   .../functional/semaphores/sem_conpro.c             |    5 +++++
   1 file changed, 5 insertions(+)
testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c +0 -0
index f5f1abf..f3a4cfb 100644
@@ -106,19 +106,23 @@ int main(int argc, char *argv[])
 
Quoted 8 lines

testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c int main(int argc, char *argv[]) · line 114

Quoted 8 lines
testcases/open_posix_testsuite/functional/semaphores/sem_conpro.c +1 -0
   #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
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list