public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v2 1/5] kernel/syscalls/ipc/lib: Don't allocate memory for getpwnam()
@ 2015-05-12  6:27 Wei,Jiangang
  2015-05-12  6:27 ` [LTP] [PATCH v2 2/5] kernel/mem/hugetlb/lib: remove unused codes " Wei,Jiangang
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Wei,Jiangang @ 2015-05-12  6:27 UTC (permalink / raw)
  To: ltp-list

The getpwnam() function returns a pointer for a passwd
structure. So,
It's enough to declare a pointer to this structure,
not to allocate memory for it.

Signed-off-by: Wei,Jiangang <weijg.fnst@cn.fujitsu.com>
---
 testcases/kernel/syscalls/ipc/lib/libipc.c | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/testcases/kernel/syscalls/ipc/lib/libipc.c b/testcases/kernel/syscalls/ipc/lib/libipc.c
index f10e257..4de7faa 100644
--- a/testcases/kernel/syscalls/ipc/lib/libipc.c
+++ b/testcases/kernel/syscalls/ipc/lib/libipc.c
@@ -143,12 +143,6 @@ int getuserid(char *user)
 {
 	struct passwd *ent;
 
-	/* allocate some space for the passwd struct */
-	if ((ent = malloc(sizeof(struct passwd))) == NULL) {
-		tst_brkm(TBROK, cleanup, "couldn't allocate space for passwd"
-			 " structure");
-	}
-
 	/* get the uid value for the user */
 	if ((ent = getpwnam(user)) == NULL) {
 		tst_brkm(TBROK, cleanup, "Couldn't get password entry for %s",
-- 
1.9.3


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v2 2/5] kernel/mem/hugetlb/lib: remove unused codes for getpwnam()
  2015-05-12  6:27 [LTP] [PATCH v2 1/5] kernel/syscalls/ipc/lib: Don't allocate memory for getpwnam() Wei,Jiangang
@ 2015-05-12  6:27 ` Wei,Jiangang
  2015-05-12  6:27 ` [LTP] [PATCH v2 3/5] kernel/syscalls/dup2: optimize allocation and free Wei,Jiangang
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Wei,Jiangang @ 2015-05-12  6:27 UTC (permalink / raw)
  To: ltp-list

No need to allocate memory for getpwnam()'s return.
so remove these unuseful codes.

Signed-off-by: Wei,Jiangang <weijg.fnst@cn.fujitsu.com>
---
 testcases/kernel/mem/hugetlb/lib/libipc.c | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/testcases/kernel/mem/hugetlb/lib/libipc.c b/testcases/kernel/mem/hugetlb/lib/libipc.c
index 21fd8da..1f16f22 100644
--- a/testcases/kernel/mem/hugetlb/lib/libipc.c
+++ b/testcases/kernel/mem/hugetlb/lib/libipc.c
@@ -85,10 +85,6 @@ int getuserid(char *user)
 {
 	struct passwd *ent;
 
-	ent = malloc(sizeof(struct passwd));
-	if (ent == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "malloc ent");
-
 	ent = getpwnam(user);
 	if (ent == NULL)
 		tst_brkm(TBROK | TERRNO, cleanup, "getpwnam");
-- 
1.9.3


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v2 3/5] kernel/syscalls/dup2: optimize allocation and free
  2015-05-12  6:27 [LTP] [PATCH v2 1/5] kernel/syscalls/ipc/lib: Don't allocate memory for getpwnam() Wei,Jiangang
  2015-05-12  6:27 ` [LTP] [PATCH v2 2/5] kernel/mem/hugetlb/lib: remove unused codes " Wei,Jiangang
@ 2015-05-12  6:27 ` Wei,Jiangang
  2015-05-12  6:27 ` [LTP] [PATCH v2 4/5] kernel/syscalls/fallocate: adjust fclose's position Wei,Jiangang
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Wei,Jiangang @ 2015-05-12  6:27 UTC (permalink / raw)
  To: ltp-list

Move the allocation and free to the cleanup and setup
respectively and ensure free fildes safely.

Signed-off-by: Wei,Jiangang <weijg.fnst@cn.fujitsu.com>
---
 testcases/kernel/syscalls/dup2/dup205.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/testcases/kernel/syscalls/dup2/dup205.c b/testcases/kernel/syscalls/dup2/dup205.c
index 14218dd..0b32453 100644
--- a/testcases/kernel/syscalls/dup2/dup205.c
+++ b/testcases/kernel/syscalls/dup2/dup205.c
@@ -40,6 +40,8 @@
 
 char *TCID = "dup205";
 int TST_TOTAL = 1;
+int *fildes;
+int min;
 int local_flag;
 
 #define PASSED 1
@@ -50,10 +52,8 @@ static void cleanup(void);
 
 int main(int ac, char *av[])
 {
-	int *fildes;
 	int ifile;
 	char pfilname[40];
-	int min;
 	int serrno;
 
 	int lc;
@@ -68,12 +68,6 @@ int main(int ac, char *av[])
 
 	for (lc = 0; TEST_LOOPING(lc); lc++) {
 
-		min = getdtablesize();	/* get number of files allowed open */
-
-		fildes = malloc((min + 10) * sizeof(int));
-		if (fildes == NULL)
-			tst_brkm(TBROK | TERRNO, cleanup, "malloc error");
-
 		sprintf(pfilname, "./dup205.%d\n", getpid());
 		unlink(pfilname);
 		serrno = 0;
@@ -125,9 +119,16 @@ int main(int ac, char *av[])
 static void setup(void)
 {
 	tst_tmpdir();
+
+	min = getdtablesize();	/* get number of files allowed open */
+	fildes = malloc((min + 10) * sizeof(int));
+	if (fildes == NULL)
+		tst_brkm(TBROK | TERRNO, cleanup, "malloc error");
 }
 
 static void cleanup(void)
 {
+	if (fildes != NULL)
+		free(fildes);
 	tst_rmdir();
 }
-- 
1.9.3


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v2 4/5] kernel/syscalls/fallocate: adjust fclose's position
  2015-05-12  6:27 [LTP] [PATCH v2 1/5] kernel/syscalls/ipc/lib: Don't allocate memory for getpwnam() Wei,Jiangang
  2015-05-12  6:27 ` [LTP] [PATCH v2 2/5] kernel/mem/hugetlb/lib: remove unused codes " Wei,Jiangang
  2015-05-12  6:27 ` [LTP] [PATCH v2 3/5] kernel/syscalls/dup2: optimize allocation and free Wei,Jiangang
@ 2015-05-12  6:27 ` Wei,Jiangang
  2015-05-12  6:27 ` [LTP] [PATCH v2 5/5] kernel/mem/mmapstress: avoid resource leak Wei,Jiangang
  2015-05-12  8:02 ` [LTP] [PATCH v2 1/5] kernel/syscalls/ipc/lib: Don't allocate memory for getpwnam() Wanlong Gao
  4 siblings, 0 replies; 6+ messages in thread
From: Wei,Jiangang @ 2015-05-12  6:27 UTC (permalink / raw)
  To: ltp-list

It will encounter resource leak for fp,
while fopen succeeds and the pointer para is NULL.
so need to move fclose out of if-block.

Signed-off-by: Wei,Jiangang <weijg.fnst@cn.fujitsu.com>
---
 testcases/kernel/sched/hyperthreading/ht_interrupt/ht_utils.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/testcases/kernel/sched/hyperthreading/ht_interrupt/ht_utils.c b/testcases/kernel/sched/hyperthreading/ht_interrupt/ht_utils.c
index 1c33d0c..bd2d373 100644
--- a/testcases/kernel/sched/hyperthreading/ht_interrupt/ht_utils.c
+++ b/testcases/kernel/sched/hyperthreading/ht_interrupt/ht_utils.c
@@ -30,8 +30,13 @@ int is_cmdline_para(const char *para)
 				return 1;
 			}
 		}
-		fclose(fp);
 	}
+	/* If fopen succeeds and the pointer para is NULL,
+	 * It won't enter the above if-block.
+	 * so need to close fp here.
+	 */
+	if (fp != NULL)
+		fclose(fp);
 
 	return 0;
 }
-- 
1.9.3


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v2 5/5] kernel/mem/mmapstress: avoid resource leak
  2015-05-12  6:27 [LTP] [PATCH v2 1/5] kernel/syscalls/ipc/lib: Don't allocate memory for getpwnam() Wei,Jiangang
                   ` (2 preceding siblings ...)
  2015-05-12  6:27 ` [LTP] [PATCH v2 4/5] kernel/syscalls/fallocate: adjust fclose's position Wei,Jiangang
@ 2015-05-12  6:27 ` Wei,Jiangang
  2015-05-12  8:02 ` [LTP] [PATCH v2 1/5] kernel/syscalls/ipc/lib: Don't allocate memory for getpwnam() Wanlong Gao
  4 siblings, 0 replies; 6+ messages in thread
From: Wei,Jiangang @ 2015-05-12  6:27 UTC (permalink / raw)
  To: ltp-list

* Avoid memory leak by allocate readbuf on the stack,
  not by malloc.

* close file descriptor explicitly before return.

Signed-off-by: Wei,Jiangang <weijg.fnst@cn.fujitsu.com>
---
 testcases/kernel/mem/mmapstress/mmapstress01.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/testcases/kernel/mem/mmapstress/mmapstress01.c b/testcases/kernel/mem/mmapstress/mmapstress01.c
index 0baf0e2..ed1466a 100644
--- a/testcases/kernel/mem/mmapstress/mmapstress01.c
+++ b/testcases/kernel/mem/mmapstress/mmapstress01.c
@@ -602,9 +602,9 @@ int fileokay(char *file, uchar_t * expbuf)
 	struct stat statbuf;
 #endif /* LARGE_FILE */
 	size_t mapsize;
-	uchar_t *readbuf;
 	unsigned mappages;
 	unsigned pagesize = sysconf(_SC_PAGE_SIZE);
+	char readbuf[pagesize];
 	int fd;
 	int cnt;
 	unsigned i, j;
@@ -641,7 +641,6 @@ int fileokay(char *file, uchar_t * expbuf)
 		perror("lseek");
 		anyfail();
 	}
-	readbuf = malloc(pagesize);
 
 	if (statbuf.st_size - sparseoffset > SIZE_MAX) {
 		fprintf(stderr, "size_t overflow when setting up map\n");
@@ -668,6 +667,7 @@ int fileokay(char *file, uchar_t * expbuf)
 				(void)fprintf(stderr, "read %d of %ld bytes\n",
 					      (i * pagesize) + cnt,
 					      (long)mapsize);
+				close(fd);
 				return 0;
 			}
 		}
@@ -688,6 +688,7 @@ int fileokay(char *file, uchar_t * expbuf)
 					      "(fsize %ld)\n", i, j,
 					      statbuf.st_size);
 #endif /* LARGE_FILE */
+				close(fd);
 				return 0;
 			}
 		}
-- 
1.9.3


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v2 1/5] kernel/syscalls/ipc/lib: Don't allocate memory for getpwnam()
  2015-05-12  6:27 [LTP] [PATCH v2 1/5] kernel/syscalls/ipc/lib: Don't allocate memory for getpwnam() Wei,Jiangang
                   ` (3 preceding siblings ...)
  2015-05-12  6:27 ` [LTP] [PATCH v2 5/5] kernel/mem/mmapstress: avoid resource leak Wei,Jiangang
@ 2015-05-12  8:02 ` Wanlong Gao
  4 siblings, 0 replies; 6+ messages in thread
From: Wanlong Gao @ 2015-05-12  8:02 UTC (permalink / raw)
  To: Wei,Jiangang; +Cc: ltp-list


Applied all, thank you.

Wanlong Gao

On 05/12/2015 02:27 PM, Wei,Jiangang wrote:
> The getpwnam() function returns a pointer for a passwd
> structure. So,
> It's enough to declare a pointer to this structure,
> not to allocate memory for it.
> 
> Signed-off-by: Wei,Jiangang <weijg.fnst@cn.fujitsu.com>
> ---
>  testcases/kernel/syscalls/ipc/lib/libipc.c | 6 ------
>  1 file changed, 6 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/ipc/lib/libipc.c b/testcases/kernel/syscalls/ipc/lib/libipc.c
> index f10e257..4de7faa 100644
> --- a/testcases/kernel/syscalls/ipc/lib/libipc.c
> +++ b/testcases/kernel/syscalls/ipc/lib/libipc.c
> @@ -143,12 +143,6 @@ int getuserid(char *user)
>  {
>  	struct passwd *ent;
>  
> -	/* allocate some space for the passwd struct */
> -	if ((ent = malloc(sizeof(struct passwd))) == NULL) {
> -		tst_brkm(TBROK, cleanup, "couldn't allocate space for passwd"
> -			 " structure");
> -	}
> -
>  	/* get the uid value for the user */
>  	if ((ent = getpwnam(user)) == NULL) {
>  		tst_brkm(TBROK, cleanup, "Couldn't get password entry for %s",
> 


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

end of thread, other threads:[~2015-05-12  8:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-05-12  6:27 [LTP] [PATCH v2 1/5] kernel/syscalls/ipc/lib: Don't allocate memory for getpwnam() Wei,Jiangang
2015-05-12  6:27 ` [LTP] [PATCH v2 2/5] kernel/mem/hugetlb/lib: remove unused codes " Wei,Jiangang
2015-05-12  6:27 ` [LTP] [PATCH v2 3/5] kernel/syscalls/dup2: optimize allocation and free Wei,Jiangang
2015-05-12  6:27 ` [LTP] [PATCH v2 4/5] kernel/syscalls/fallocate: adjust fclose's position Wei,Jiangang
2015-05-12  6:27 ` [LTP] [PATCH v2 5/5] kernel/mem/mmapstress: avoid resource leak Wei,Jiangang
2015-05-12  8:02 ` [LTP] [PATCH v2 1/5] kernel/syscalls/ipc/lib: Don't allocate memory for getpwnam() Wanlong Gao

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