Fix error path of main thread with freeing allocated buffer 02/190802/3 accepted/tizen/5.0/unified/20181102.021321 accepted/tizen/unified/20181011.095115 submit/tizen/20181010.051409 submit/tizen_5.0/20181101.000004
authorSeung-Woo Kim <sw0312.kim@samsung.com>
Fri, 5 Oct 2018 09:46:41 +0000 (18:46 +0900)
committerSeung-Woo Kim <sw0312.kim@samsung.com>
Fri, 5 Oct 2018 11:49:53 +0000 (20:49 +0900)
There can be memory leak for strdup(). Fix error path of main
thread with freeing allocated buffer.

Change-Id: I292d66d8b9f5c857a692f35a9867d221dabb6a7d
Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
src/main.c

index 0b86091..194e2c8 100644 (file)
@@ -27,6 +27,7 @@ int _main(int argc, char *argv[])
 {
        struct tfm_context ctx;
        const char *part_table = "/usr/share/partition.info";
+       char *opt_table = NULL;
        int ret, opt;
 
        memset(&ctx, 0, sizeof(ctx));
@@ -35,11 +36,15 @@ int _main(int argc, char *argv[])
                switch (opt) {
                case 'i':
                        if (optarg) {
-                               part_table = strdup(optarg);
+                               if (opt_table)
+                                       free(opt_table);
+                               opt_table = strdup(optarg);
+                               part_table = opt_table;
                        } else {
                                fprintf(stderr,
                                        "path should be specified with '-i'\n");
-                               exit(-1);
+                               ret = -1;
+                               goto out;
                        }
                        break;
                case 'p':
@@ -51,43 +56,54 @@ int _main(int argc, char *argv[])
                        if (*optarg == '\0' || (endptr && *endptr != '\0')) {
                                fprintf(stderr,
                                        "value should be provided as a number for '-p'\n");
-                               exit(-1);
+                               ret = -1;
+                               goto out_optfree;
                        }
                        ctx.port = (int)val;
                        break;
                }
                default:
-                       return 0;
+                       ret = -1;
+                       goto out_optfree;
                }
        }
 
        ret = dfu_init(&ctx, part_table);
-       if (ret < 0)
-               exit(-1);
+       if (ret < 0) {
+               ret = -1;
+               goto out_optfree;
+       }
 
        ret = net_connect(&ctx);
        if (ret < 0) {
-               dfu_exit(&ctx);
-               exit(-1);
+               ret = -1;
+               goto out_dfuexit;
        }
 
        ret = thor_init(&ctx);
        if (ret < 0) {
-               net_disconnect(&ctx);
-               dfu_exit(&ctx);
-               exit(-1);
+               ret = -1;
+               goto out_netdisconn;
        }
 
        ret = thor_process(&ctx);
        if (ret < 0) {
-               net_disconnect(&ctx);
-               dfu_exit(&ctx);
-               exit(-1);
+               ret = -1;
+               goto out_netdisconn;
        }
 
+out_netdisconn:
        net_disconnect(&ctx);
+
+out_dfuexit:
        dfu_exit(&ctx);
-       exit(0);
+
+out_optfree:
+       if (opt_table)
+               free(opt_table);
+
+out:
+       exit(ret);
 }
 
 int main(int argc, char *argv[])