The expanded string length must include the trailing null character for 'magic' environment variables too.
Signed-off-by: Francois Gouget fgouget@codeweavers.com --- Compare the lstrlenW() return value with ExpandEnvironmentStringsW()'s. --- programs/cmd/tests/test_builtins.cmd.exp | 4 ++-- programs/cmd/wcmdmain.c | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/programs/cmd/tests/test_builtins.cmd.exp b/programs/cmd/tests/test_builtins.cmd.exp index 9075b4e3246..00224d033c6 100644 --- a/programs/cmd/tests/test_builtins.cmd.exp +++ b/programs/cmd/tests/test_builtins.cmd.exp @@ -1219,12 +1219,12 @@ WINE_bar correctly 6@or_broken@ERROR: WINE_bar incorrectly 5 [6] @todo_wine@Has %cd% CD value @pwd@@or_broken@CD value@space@ @todo_wine@Has %date% -@todo_wine@Good %date% +Good %date% @todo_wine@Match @todo_wine@Has %errorlevel% @todo_wine@Has %time% %time% has seconds -@todo_wine@%time% has 1/100s +%time% has 1/100s End of %time% @todo_wine@Match @todo_wine@Has %random% diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c index 318e8e6e454..61812b7a7d4 100644 --- a/programs/cmd/wcmdmain.c +++ b/programs/cmd/wcmdmain.c @@ -615,11 +615,11 @@ static WCHAR *WCMD_expand_envvar(WCHAR *start, WCHAR startchar) /* override if existing env var called that name */ if (WCMD_is_magic_envvar(thisVar, L"ERRORLEVEL")) { wsprintfW(thisVarContents, L"%d", errorlevel); - len = lstrlenW(thisVarContents); + len = lstrlenW(thisVarContents) + 1; } else if (WCMD_is_magic_envvar(thisVar, L"DATE")) { GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, thisVarContents, MAXSTRING); - len = lstrlenW(thisVarContents); + len = lstrlenW(thisVarContents) + 1; } else if (WCMD_is_magic_envvar(thisVar, L"TIME")) { GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, NULL, thisVarContents, MAXSTRING); @@ -627,13 +627,13 @@ static WCHAR *WCMD_expand_envvar(WCHAR *start, WCHAR startchar) * separator. */ if (*thisVarContents == '0') *thisVarContents = ' '; lstrcatW(thisVarContents, L".00"); - len = lstrlenW(thisVarContents); + len = lstrlenW(thisVarContents) + 1; } else if (WCMD_is_magic_envvar(thisVar, L"CD")) { GetCurrentDirectoryW(MAXSTRING, thisVarContents); - len = lstrlenW(thisVarContents); + len = lstrlenW(thisVarContents) + 1; } else if (WCMD_is_magic_envvar(thisVar, L"RANDOM")) { wsprintfW(thisVarContents, L"%d", rand() % 32768); - len = lstrlenW(thisVarContents); + len = lstrlenW(thisVarContents) + 1; } else {
len = ExpandEnvironmentStringsW(thisVar, thisVarContents, ARRAY_SIZE(thisVarContents));