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 | 7 +++++++ programs/cmd/wcmdmain.c | 10 +++++----- 2 files changed, 12 insertions(+), 5 deletions(-)
diff --git a/programs/cmd/tests/test_builtins.cmd b/programs/cmd/tests/test_builtins.cmd index 8133268d2dd..d8150692b54 100644 --- a/programs/cmd/tests/test_builtins.cmd +++ b/programs/cmd/tests/test_builtins.cmd @@ -1791,9 +1791,16 @@ call :checkenvvars WINE_foo 6 WINE_bar 6
echo --- Magic environment variables echo Century=%date:~6,2% +set WINE_foo=%date:~9,1% +if not defined WINE_foo echo "%%date%% is too short" +rem But may be longer depending on locale
set WINE_foo=%time:~7,1% if not defined WINE_foo echo "No seconds in %%time%%" +set WINE_foo=%time:~10,1% +if not defined WINE_foo echo "No 1/100s in %%time%%" +set WINE_foo=%time:~11,1% +if defined WINE_foo echo "Found more time at the end of %%time%%"
set WINE_foo=%random% if %WINE_foo% equ %random% echo "%%random%% is not random" diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c index cd083449ffe..fbded294741 100644 --- a/programs/cmd/wcmdmain.c +++ b/programs/cmd/wcmdmain.c @@ -615,24 +615,24 @@ 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); /* FIXME This should have 1/100s precision as well as a localized * decimal separator */ 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));