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

Signed-off-by: Wei,Jiangang <weijg.f...@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 via email to