parseArguments: avoid segmentation fault 94/206494/1
authorHeinrich Schuchardt <xypron.glpk@gmx.de>
Sat, 2 Mar 2019 07:36:46 +0000 (08:36 +0100)
committerAdam Malinowski <a.malinowsk2@partner.samsung.com>
Mon, 20 May 2019 09:52:51 +0000 (11:52 +0200)
If an incorrect command line argument is provided poptBadOption() is
called. Freeing the context optCon before this call leads to a
segmentation fault.

If no command line argument is provided we should free the context after
displaying the usage information to avoid a memory leak.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
src/main.cpp

index 4c6e917..43b74a8 100644 (file)
@@ -709,6 +709,7 @@ int parseArguments(int argc, const char **argv, CCCommand *cmd, int *arg, char *
     poptSetOtherOptionHelp(optCon, "command");
     if (argc < 2) {
         poptPrintUsage(optCon, stderr, 0);
+        poptFreeContext(optCon);
         return EXIT_SUCCESS;
     }
     /* Now do options processing, get portname */
@@ -756,17 +757,18 @@ int parseArguments(int argc, const char **argv, CCCommand *cmd, int *arg, char *
         }
     }
 
-    poptFreeContext(optCon);
-
     if (serial)
         snprintf(args, argsLen, "%s", serial);
     free(serial);
 
     if (c < -1) {
         fprintf(stderr, "%s: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(c));
+        poptFreeContext(optCon);
         return EXIT_FAILURE;
     }
 
+    poptFreeContext(optCon);
+
     return EXIT_SUCCESS;
 }