test : free after using it 05/198105/1
authorKichan Kwon <k_c.kwon@samsung.com>
Mon, 21 Jan 2019 02:11:53 +0000 (11:11 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Mon, 21 Jan 2019 02:18:30 +0000 (11:18 +0900)
Change-Id: I1b982282a025d58b5adf3972123016a2e7652490
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
test/runtime_info_test_resourced.c

index 15135f5..ea90fee 100644 (file)
@@ -57,9 +57,9 @@ int runtime_info_test_helper_check_return_value(const char *name, int ret)
 void runtime_info_test_get_process_memory_info(pid_t pid)
 {
        int ret;
-       process_memory_info_s *info;
+       process_memory_info_s *info = NULL;
        char buf[BUF_SIZE];
-       FILE *fp;
+       FILE *fp = NULL, *pp = NULL;
        unsigned long vsz;
        int code, data, pss;
 
@@ -75,7 +75,7 @@ void runtime_info_test_get_process_memory_info(pid_t pid)
        if (!fp) {
                printf("Failed to open %s\n", buf);
                printf("  Result : %s\n", PRINT_RESULT(ERROR));
-               return;
+               goto error;
        }
 
        while (fgets(buf, BUF_SIZE, fp)) {
@@ -83,30 +83,24 @@ void runtime_info_test_get_process_memory_info(pid_t pid)
                                        &vsz) != 1) {
                        printf("/proc/%u/stat has invalid format\n", pid);
                        printf("  Result : %s\n", PRINT_RESULT(ERROR));
-                       fclose(fp);
-                       return;
+                       goto error;
                }
        }
 
-       fclose(fp);
-
        snprintf(buf, BUF_SIZE, "memps -a | grep runtime-info-test | grep %u", pid);
-       fp = popen(buf, "r");
-       if (!fp) {
+       pp = popen(buf, "r");
+       if (!pp) {
                printf("popen failed\n");
                printf("  Result : %s\n", PRINT_RESULT(ERROR));
-               return;
+               goto error;
        }
 
-       if (!fscanf(fp, "%*d %d %d %*d %d", &code, &data, &pss)) {
+       if (!fscanf(pp, "%*d %d %d %*d %d", &code, &data, &pss)) {
                printf("memps has invalid format\n");
                printf("  Result : %s\n", PRINT_RESULT(ERROR));
-               pclose(fp);
-               return;
+               goto error;
        }
 
-       pclose(fp);
-
        printf("  Virtual memory size   : %s\n",
                        PRINT_RESULT(check_value(info[0].vsz, BytetoKiB(vsz), 10)));
        printf("  Proportional set size : %s\n",
@@ -117,14 +111,22 @@ void runtime_info_test_get_process_memory_info(pid_t pid)
        printf("  Dirty memory size     : %s\n",
                        PRINT_RESULT(check_value(info[0].shared_dirty + info[0].private_dirty,
                                                                        data, 10)));
+
+error:
+       if (pp)
+               pclose(pp);
+       if (fp)
+               fclose(fp);
+       if (info)
+               free(info);
 }
 
 void runtime_info_test_get_process_cpu_usage(pid_t pid)
 {
        int ret;
-       process_cpu_usage_s *usage;
+       process_cpu_usage_s *usage = NULL;
        char buf[BUF_SIZE];
-       FILE *fp;
+       FILE *fp = NULL;
        unsigned long utime, stime;
 
        printf("Runtime_info_get_process_cpu_usage test\n");
@@ -138,7 +140,7 @@ void runtime_info_test_get_process_cpu_usage(pid_t pid)
        if (!fp) {
                printf("Failed to open %s\n", buf);
                printf("  Result : %s\n", PRINT_RESULT(ERROR));
-               return;
+               goto error;
        }
 
        while (fgets(buf, BUF_SIZE, fp)) {
@@ -146,17 +148,20 @@ void runtime_info_test_get_process_cpu_usage(pid_t pid)
                                        &utime, &stime) != 2) {
                        printf("/proc/%u/stat has invalid format\n", pid);
                        printf("  Result : %s\n", PRINT_RESULT(ERROR));
-                       fclose(fp);
-                       return;
+                       goto error;
                }
        }
 
-       fclose(fp);
-
        printf("  User time   : %s\n",
                        PRINT_RESULT(check_value(usage[0].utime, utime, 0)));
        printf("  System time : %s\n",
                        PRINT_RESULT(check_value(usage[0].stime, stime, 0)));
+
+error:
+       if (fp)
+               fclose(fp);
+       if (usage)
+               free(usage);
 }
 
 static int runtime_info_test_helper_find_pid(app_usage_h *h, pid_t *pid)