[PATCH 3/5 v2] cmd: Fix an off-by-one error in WCMD_expand_envvar().

Francois Gouget fgouget at codeweavers.com
Fri Mar 25 10:15:31 CDT 2022


The expanded string length must include the trailing null character for
'magic' environment variables too.

Signed-off-by: Francois Gouget <fgouget at codeweavers.com>
---
v2: No change.

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 at or_broken@ERROR: WINE_bar incorrectly 5 [6]
 @todo_wine at Has %cd%
 CD value @pwd@@or_broken at CD value at space@
 @todo_wine at Has %date%
- at todo_wine@Good %date%
+Good %date%
 @todo_wine at Match
 @todo_wine at Has %errorlevel%
 @todo_wine at Has %time%
 %time% has seconds
- at todo_wine@%time% has 1/100s
+%time% has 1/100s
 End of %time%
 @todo_wine at Match
 @todo_wine at 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));
-- 
2.30.2




More information about the wine-devel mailing list