Commit 7f60584a authored by Eric Pouech's avatar Eric Pouech Committed by Alexandre Julliard

cmd: Fix substring expansion for 'magic' variables.

parent b868d823
...@@ -533,7 +533,7 @@ e@or_broken@qwerty ...@@ -533,7 +533,7 @@ e@or_broken@qwerty
''@or_broken@'qwerty' ''@or_broken@'qwerty'
r@or_broken@qwerty r@or_broken@qwerty
ty ty
@todo_wine@mmydir mmydir
------------ Testing variable substitution ------------ ------------ Testing variable substitution ------------
--- in FOR variables --- in FOR variables
"A B" "A B"
......
...@@ -614,25 +614,19 @@ static WCHAR *WCMD_expand_envvar(WCHAR *start, WCHAR startchar) ...@@ -614,25 +614,19 @@ static WCHAR *WCMD_expand_envvar(WCHAR *start, WCHAR startchar)
/* Handle DATE, TIME, ERRORLEVEL and CD replacements allowing */ /* Handle DATE, TIME, ERRORLEVEL and CD replacements allowing */
/* override if existing env var called that name */ /* override if existing env var called that name */
if (WCMD_is_magic_envvar(thisVar, L"ERRORLEVEL")) { if (WCMD_is_magic_envvar(thisVar, L"ERRORLEVEL")) {
wsprintfW(thisVarContents, L"%d", errorlevel); len = wsprintfW(thisVarContents, L"%d", errorlevel);
len = lstrlenW(thisVarContents);
} else if (WCMD_is_magic_envvar(thisVar, L"DATE")) { } else if (WCMD_is_magic_envvar(thisVar, L"DATE")) {
GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, len = GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL,
NULL, thisVarContents, MAXSTRING); NULL, thisVarContents, ARRAY_SIZE(thisVarContents));
len = lstrlenW(thisVarContents);
} else if (WCMD_is_magic_envvar(thisVar, L"TIME")) { } else if (WCMD_is_magic_envvar(thisVar, L"TIME")) {
GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, NULL, len = GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, NULL,
NULL, thisVarContents, MAXSTRING); NULL, thisVarContents, ARRAY_SIZE(thisVarContents));
len = lstrlenW(thisVarContents);
} else if (WCMD_is_magic_envvar(thisVar, L"CD")) { } else if (WCMD_is_magic_envvar(thisVar, L"CD")) {
GetCurrentDirectoryW(MAXSTRING, thisVarContents); len = GetCurrentDirectoryW(ARRAY_SIZE(thisVarContents), thisVarContents);
len = lstrlenW(thisVarContents);
} else if (WCMD_is_magic_envvar(thisVar, L"RANDOM")) { } else if (WCMD_is_magic_envvar(thisVar, L"RANDOM")) {
wsprintfW(thisVarContents, L"%d", rand() % 32768); len = wsprintfW(thisVarContents, L"%d", rand() % 32768);
len = lstrlenW(thisVarContents);
} else { } else {
if ((len = ExpandEnvironmentStringsW(thisVar, thisVarContents, ARRAY_SIZE(thisVarContents)))) len--;
len = ExpandEnvironmentStringsW(thisVar, thisVarContents, ARRAY_SIZE(thisVarContents));
} }
if (len == 0) if (len == 0)
...@@ -704,9 +698,9 @@ static WCHAR *WCMD_expand_envvar(WCHAR *start, WCHAR startchar) ...@@ -704,9 +698,9 @@ static WCHAR *WCMD_expand_envvar(WCHAR *start, WCHAR startchar)
/* Check bounds */ /* Check bounds */
if (substrposition >= 0) { if (substrposition >= 0) {
startCopy = &thisVarContents[min(substrposition, len)]; startCopy = &thisVarContents[min(substrposition, len - 1)];
} else { } else {
startCopy = &thisVarContents[max(0, len+substrposition-1)]; startCopy = &thisVarContents[max(0, len + substrposition)];
} }
if (commapos == NULL) { if (commapos == NULL) {
...@@ -714,12 +708,12 @@ static WCHAR *WCMD_expand_envvar(WCHAR *start, WCHAR startchar) ...@@ -714,12 +708,12 @@ static WCHAR *WCMD_expand_envvar(WCHAR *start, WCHAR startchar)
WCMD_strsubstW(start, endOfVar + 1, startCopy, -1); WCMD_strsubstW(start, endOfVar + 1, startCopy, -1);
} else if (substrlength < 0) { } else if (substrlength < 0) {
int copybytes = (len+substrlength-1)-(startCopy-thisVarContents); int copybytes = len + substrlength - (startCopy - thisVarContents);
if (copybytes > len) copybytes = len; if (copybytes >= len) copybytes = len - 1;
else if (copybytes < 0) copybytes = 0; else if (copybytes < 0) copybytes = 0;
WCMD_strsubstW(start, endOfVar + 1, startCopy, copybytes); WCMD_strsubstW(start, endOfVar + 1, startCopy, copybytes);
} else { } else {
substrlength = min(substrlength, len - (startCopy- thisVarContents + 1)); substrlength = min(substrlength, len - (startCopy - thisVarContents));
WCMD_strsubstW(start, endOfVar + 1, startCopy, substrlength); WCMD_strsubstW(start, endOfVar + 1, startCopy, substrlength);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment