fix [winpr/util]: memory leak in TestCmdLine
authorBernhard Miklautz <bernhard.miklautz@thincast.com>
Mon, 5 Nov 2018 11:29:06 +0000 (12:29 +0100)
committerBernhard Miklautz <bernhard.miklautz@thincast.com>
Mon, 5 Nov 2018 11:29:06 +0000 (12:29 +0100)
winpr/libwinpr/utils/test/TestCmdLine.c

index dc85f6b..c853038 100644 (file)
@@ -60,6 +60,7 @@ static COMMAND_LINE_ARGUMENT_A args[] =
 int TestCmdLine(int argc, char* argv[])
 {
        int status;
+       int ret = -1;
        DWORD flags;
        long width = 0;
        long height = 0;
@@ -70,12 +71,19 @@ int TestCmdLine(int argc, char* argv[])
        flags = COMMAND_LINE_SIGIL_SLASH | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_SIGIL_PLUS_MINUS;
         testArgc = string_list_length(testArgv);
        command_line = string_list_copy(testArgv);
+
+       if (!command_line)
+       {
+               printf("Argument duplication failed (not enough memory?)\n");
+               return ret;
+       }
+
        status = CommandLineParseArgumentsA(testArgc, command_line, args, flags, NULL, NULL, NULL);
 
        if (status != 0)
        {
                printf("CommandLineParseArgumentsA failure: %d\n", status);
-               return -1;
+               goto out;
        }
 
        arg = CommandLineFindArgumentA(args, "w");
@@ -83,7 +91,7 @@ int TestCmdLine(int argc, char* argv[])
        if (strcmp("1024", arg->Value) != 0)
        {
                printf("CommandLineFindArgumentA: unexpected %s value %s\n", arg->Name, arg->Value);
-               return -1;
+               goto out;
        }
 
        arg = CommandLineFindArgumentA(args, "h");
@@ -91,7 +99,7 @@ int TestCmdLine(int argc, char* argv[])
        if (strcmp("768", arg->Value) != 0)
        {
                printf("CommandLineFindArgumentA: unexpected %s value %s\n", arg->Name, arg->Value);
-               return -1;
+               goto out;
        }
 
        arg = CommandLineFindArgumentA(args, "f");
@@ -99,7 +107,7 @@ int TestCmdLine(int argc, char* argv[])
        if (arg->Value)
        {
                printf("CommandLineFindArgumentA: unexpected %s value\n", arg->Name);
-               return -1;
+               goto out;
        }
 
        arg = CommandLineFindArgumentA(args, "admin");
@@ -107,7 +115,7 @@ int TestCmdLine(int argc, char* argv[])
        if (!arg->Value)
        {
                printf("CommandLineFindArgumentA: unexpected %s value\n", arg->Name);
-               return -1;
+               goto out;
        }
 
        arg = CommandLineFindArgumentA(args, "multimon");
@@ -115,7 +123,7 @@ int TestCmdLine(int argc, char* argv[])
        if (!arg->Value)
        {
                printf("CommandLineFindArgumentA: unexpected %s value\n", arg->Name);
-               return -1;
+               goto out;
        }
 
        arg = CommandLineFindArgumentA(args, "v");
@@ -123,7 +131,7 @@ int TestCmdLine(int argc, char* argv[])
        if (strcmp("localhost:3389", arg->Value) != 0)
        {
                printf("CommandLineFindArgumentA: unexpected %s value %s\n", arg->Name, arg->Value);
-               return -1;
+               goto out;
        }
 
        arg = CommandLineFindArgumentA(args, "fonts");
@@ -131,7 +139,7 @@ int TestCmdLine(int argc, char* argv[])
        if (!arg->Value)
        {
                printf("CommandLineFindArgumentA: unexpected %s value\n", arg->Name);
-               return -1;
+               goto out;
        }
 
        arg = CommandLineFindArgumentA(args, "wallpaper");
@@ -139,7 +147,7 @@ int TestCmdLine(int argc, char* argv[])
        if (arg->Value)
        {
                printf("CommandLineFindArgumentA: unexpected %s value\n", arg->Name);
-               return -1;
+               goto out;
        }
 
        arg = CommandLineFindArgumentA(args, "help");
@@ -147,7 +155,7 @@ int TestCmdLine(int argc, char* argv[])
        if (arg->Value)
        {
                printf("CommandLineFindArgumentA: unexpected %s value\n", arg->Name);
-               return -1;
+               goto out;
        }
 
        arg = args;
@@ -168,14 +176,14 @@ int TestCmdLine(int argc, char* argv[])
                        width = strtol(arg->Value, NULL, 0);
 
                        if (errno != 0)
-                               return -1;
+                               goto out;
                }
                CommandLineSwitchCase(arg, "h")
                {
                        height = strtol(arg->Value, NULL, 0);
 
                        if (errno != 0)
-                               return -1;
+                               goto out;
                }
                CommandLineSwitchDefault(arg)
                {
@@ -187,8 +195,11 @@ int TestCmdLine(int argc, char* argv[])
        if ((width != 1024) || (height != 768))
        {
                printf("Unexpected width and height: Actual: (%ldx%ld), Expected: (1024x768)\n", width, height);
-               return -1;
+               goto out;
        }
+       ret = 0;
 
-       return 0;
+out:
+    string_list_free(command_line);
+    return ret;
 }