From 2d582ff542c97227042fadf65776d7dd1a2846e4 Mon Sep 17 00:00:00 2001 From: Mark Jansen Date: Wed, 21 Sep 2016 19:34:42 +0000 Subject: [PATCH 1/1] [CRT] Fix __getmainargs parsing for a commandline ending with spaces. Patch by Yaroslav Veremenko CORE-9199 #comment Thanks, please re-test! svn path=/trunk/; revision=72763 --- reactos/sdk/lib/crt/misc/getargs.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/reactos/sdk/lib/crt/misc/getargs.c b/reactos/sdk/lib/crt/misc/getargs.c index a80d815a0ff..df20bb227ca 100644 --- a/reactos/sdk/lib/crt/misc/getargs.c +++ b/reactos/sdk/lib/crt/misc/getargs.c @@ -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) { - int i, doexpand, slashesAdded, escapedQuote, inQuotes, bufferIndex; + int i, doexpand, slashesAdded, escapedQuote, inQuotes, bufferIndex, anyLetter; 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; + anyLetter = FALSE; 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)) { - 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) { @@ -222,12 +227,15 @@ void __getmainargs(int* argc, char*** argv, char*** env, int expand_wildcards, i while (_acmdln[i] == ' ' || _acmdln[i] == '\t') ++i; + anyLetter = FALSE; bufferIndex = 0; slashesAdded = 0; escapedQuote = FALSE; continue; } + anyLetter = TRUE; + 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) { - int i, doexpand, slashesAdded, escapedQuote, inQuotes, bufferIndex; + int i, doexpand, slashesAdded, escapedQuote, inQuotes, bufferIndex, anyLetter; 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; + anyLetter = TRUE; 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)) { - 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) { @@ -348,12 +361,15 @@ void __wgetmainargs(int* argc, wchar_t*** wargv, wchar_t*** wenv, while (_wcmdln[i] == ' ' || _wcmdln[i] == '\t') ++i; + anyLetter = FALSE; bufferIndex = 0; slashesAdded = 0; escapedQuote = FALSE; continue; } + anyLetter = TRUE; + if (_wcmdln[i] == '\\') { buffer[bufferIndex++] = _wcmdln[i]; -- 2.17.1