[CRT] Fix __getmainargs parsing for a commandline ending with spaces. Patch by Yarosl...
authorMark Jansen <mark.jansen@reactos.org>
Wed, 21 Sep 2016 19:34:42 +0000 (19:34 +0000)
committerMark Jansen <mark.jansen@reactos.org>
Wed, 21 Sep 2016 19:34:42 +0000 (19:34 +0000)
svn path=/trunk/; revision=72763

reactos/sdk/lib/crt/misc/getargs.c

index a80d815..df20bb2 100644 (file)
@@ -181,7 +181,7 @@ int aexpand(char* name, int expand_wildcards)
  */
 void __getmainargs(int* argc, char*** argv, char*** env, int expand_wildcards, int* new_mode)
 {
  */
 void __getmainargs(int* argc, char*** argv, char*** env, int expand_wildcards, int* new_mode)
 {
-   int i, doexpand, slashesAdded, escapedQuote, inQuotes, bufferIndex;
+   int i, doexpand, slashesAdded, escapedQuote, inQuotes, bufferIndex, anyLetter;
    size_t len;
    char* buffer;
 
    size_t len;
    char* buffer;
 
@@ -190,6 +190,7 @@ void __getmainargs(int* argc, char*** argv, char*** env, int expand_wildcards, i
    i = 0;
    doexpand = expand_wildcards;
    escapedQuote = FALSE;
    i = 0;
    doexpand = expand_wildcards;
    escapedQuote = FALSE;
+   anyLetter = FALSE;
    slashesAdded = 0;
    inQuotes = 0;
    bufferIndex = 0;
    slashesAdded = 0;
    inQuotes = 0;
    bufferIndex = 0;
@@ -213,7 +214,11 @@ void __getmainargs(int* argc, char*** argv, char*** env, int expand_wildcards, i
       // Arguments are delimited by white space, which is either a space or a tab.
       if (i >= len || ((_acmdln[i] == ' ' || _acmdln[i] == '\t') && !inQuotes))
       {
       // Arguments are delimited by white space, which is either a space or a tab.
       if (i >= len || ((_acmdln[i] == ' ' || _acmdln[i] == '\t') && !inQuotes))
       {
-         aexpand(strndup(buffer, bufferIndex), doexpand);
+         // Handle the case when empty spaces are in the end of the cmdline
+         if (anyLetter) 
+         {
+            aexpand(strndup(buffer, bufferIndex), doexpand);
+         }
          // Copy the last element from buffer and quit the loop
          if (i >= len)
          {
          // Copy the last element from buffer and quit the loop
          if (i >= len)
          {
@@ -222,12 +227,15 @@ void __getmainargs(int* argc, char*** argv, char*** env, int expand_wildcards, i
 
          while (_acmdln[i] == ' ' || _acmdln[i] == '\t')
             ++i;
 
          while (_acmdln[i] == ' ' || _acmdln[i] == '\t')
             ++i;
+         anyLetter = FALSE;
          bufferIndex = 0;
          slashesAdded = 0;
          escapedQuote = FALSE;
          continue;
       }
 
          bufferIndex = 0;
          slashesAdded = 0;
          escapedQuote = FALSE;
          continue;
       }
 
+      anyLetter = TRUE;
+
       if (_acmdln[i] == '\\')
       {
          buffer[bufferIndex++] = _acmdln[i];
       if (_acmdln[i] == '\\')
       {
          buffer[bufferIndex++] = _acmdln[i];
@@ -307,7 +315,7 @@ void __getmainargs(int* argc, char*** argv, char*** env, int expand_wildcards, i
 void __wgetmainargs(int* argc, wchar_t*** wargv, wchar_t*** wenv,
                     int expand_wildcards, int* new_mode)
 {
 void __wgetmainargs(int* argc, wchar_t*** wargv, wchar_t*** wenv,
                     int expand_wildcards, int* new_mode)
 {
-   int i, doexpand, slashesAdded, escapedQuote, inQuotes, bufferIndex;
+   int i, doexpand, slashesAdded, escapedQuote, inQuotes, bufferIndex, anyLetter;
    size_t len;
    wchar_t* buffer;
 
    size_t len;
    wchar_t* buffer;
 
@@ -316,6 +324,7 @@ void __wgetmainargs(int* argc, wchar_t*** wargv, wchar_t*** wenv,
    i = 0;
    doexpand = expand_wildcards;
    escapedQuote = FALSE;
    i = 0;
    doexpand = expand_wildcards;
    escapedQuote = FALSE;
+   anyLetter = TRUE;
    slashesAdded = 0;
    inQuotes = 0;
    bufferIndex = 0;
    slashesAdded = 0;
    inQuotes = 0;
    bufferIndex = 0;
@@ -339,7 +348,11 @@ void __wgetmainargs(int* argc, wchar_t*** wargv, wchar_t*** wenv,
       // Arguments are delimited by white space, which is either a space or a tab.
       if (i >= len || ((_wcmdln[i] == ' ' || _wcmdln[i] == '\t') && !inQuotes))
       {
       // Arguments are delimited by white space, which is either a space or a tab.
       if (i >= len || ((_wcmdln[i] == ' ' || _wcmdln[i] == '\t') && !inQuotes))
       {
-         wexpand(wcsndup(buffer, bufferIndex), doexpand);
+         // Handle the case when empty spaces are in the end of the cmdline
+         if (anyLetter)
+         {
+            wexpand(wcsndup(buffer, bufferIndex), doexpand);
+         }
          // Copy the last element from buffer and quit the loop
          if (i >= len)
          {
          // Copy the last element from buffer and quit the loop
          if (i >= len)
          {
@@ -348,12 +361,15 @@ void __wgetmainargs(int* argc, wchar_t*** wargv, wchar_t*** wenv,
 
          while (_wcmdln[i] == ' ' || _wcmdln[i] == '\t')
             ++i;
 
          while (_wcmdln[i] == ' ' || _wcmdln[i] == '\t')
             ++i;
+         anyLetter = FALSE;
          bufferIndex = 0;
          slashesAdded = 0;
          escapedQuote = FALSE;
          continue;
       }
 
          bufferIndex = 0;
          slashesAdded = 0;
          escapedQuote = FALSE;
          continue;
       }
 
+      anyLetter = TRUE;
+
       if (_wcmdln[i] == '\\')
       {
          buffer[bufferIndex++] = _wcmdln[i];
       if (_wcmdln[i] == '\\')
       {
          buffer[bufferIndex++] = _wcmdln[i];