All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Wei,Jiangang" <weijg.fnst@cn.fujitsu.com>
To: ltp-list@lists.sourceforge.net
Subject: [LTP] [PATCH v2 4/5] kernel/syscalls/dup2: optimize allocation and free
Date: Fri, 8 May 2015 11:33:23 +0800	[thread overview]
Message-ID: <1431056003-11503-1-git-send-email-weijg.fnst@cn.fujitsu.com> (raw)
In-Reply-To: <20150507132611.GF28637@rei.suse.de>

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

  reply	other threads:[~2015-05-08  3:34 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-06  7:26 [LTP] [PATCH 1/5] kernel/syscalls/mmap: remove unused cleanup() Wei,Jiangang
2015-05-06  7:26 ` [LTP] [PATCH 2/5] kernel/syscalls/ipc/lib: Don't allocate memory for getpwnam() Wei,Jiangang
2015-05-07 13:21   ` Cyril Hrubis
2015-05-08  3:28     ` [LTP] [PATCH v2 " Wei,Jiangang
2015-05-06  7:26 ` [LTP] [PATCH 3/5] kernel/mem/hugetlb/lib: " Wei,Jiangang
2015-05-07 13:21   ` Cyril Hrubis
2015-05-08  3:31     ` [LTP] [PATCH v2 " Wei,Jiangang
2015-05-08  8:33     ` [LTP] [PATCH v3 3/5] kernel/mem/hugetlb/lib: do refactor for getuserid Wei,Jiangang
2015-05-06  7:26 ` [LTP] [PATCH 4/5] kernel/syscalls/dup2: free fildes to avoid memory leak Wei,Jiangang
2015-05-07 13:26   ` Cyril Hrubis
2015-05-08  3:33     ` Wei,Jiangang [this message]
2015-05-06  7:26 ` [LTP] [PATCH 5/5] kernel/syscalls/fallocate: move fclose out of if-block Wei,Jiangang
2015-05-07 13:32   ` Cyril Hrubis
2015-05-08  3:39     ` [LTP] [PATCH v2 5/5] kernel/syscalls/fallocate: adjust fclose's position Wei,Jiangang
2015-05-07 13:22 ` [LTP] [PATCH 1/5] kernel/syscalls/mmap: remove unused cleanup() Cyril Hrubis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1431056003-11503-1-git-send-email-weijg.fnst@cn.fujitsu.com \
    --to=weijg.fnst@cn.fujitsu.com \
    --cc=ltp-list@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.