[PATCH 1/2] cmd: Use terminated strings in WCMD_ReadAndParseLine().

Gijs Vermeulen gijsvrm at gmail.com
Sun Apr 11 19:22:36 CDT 2021


Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=48486
Signed-off-by: Gijs Vermeulen <gijsvrm at gmail.com>
---
 programs/cmd/wcmdmain.c | 42 +++++++++++++++--------------------------
 1 file changed, 15 insertions(+), 27 deletions(-)

diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c
index dd7d14e50e5..2260ed9da35 100644
--- a/programs/cmd/wcmdmain.c
+++ b/programs/cmd/wcmdmain.c
@@ -1813,11 +1813,6 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE
     CMD_LIST *lastEntry = NULL;
     CMD_DELIMITERS prevDelim = CMD_NONE;
     static WCHAR    *extraSpace = NULL;  /* Deliberately never freed */
-    static const WCHAR remCmd[] = {'r','e','m'};
-    static const WCHAR forCmd[] = {'f','o','r'};
-    static const WCHAR ifCmd[]  = {'i','f'};
-    static const WCHAR ifElse[] = {'e','l','s','e'};
-    static const WCHAR setCmd[] = {'s','e','t'};
     BOOL      inOneLine = FALSE;
     BOOL      inFor = FALSE;
     BOOL      inIn  = FALSE;
@@ -1870,10 +1865,7 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE
 
     /* Show prompt before batch line IF echo is on and in batch program */
     if (context && echo_mode && *curPos && (*curPos != '@')) {
-      static const WCHAR echoDot[] = {'e','c','h','o','.'};
-      static const WCHAR echoCol[] = {'e','c','h','o',':'};
-      static const WCHAR echoSlash[] = {'e','c','h','o','/'};
-      const DWORD len = ARRAY_SIZE(echoDot);
+      const DWORD len = ARRAY_SIZE(L"echo.") - 1;
       DWORD curr_size = lstrlenW(curPos);
       DWORD min_len = (curr_size < len ? curr_size : len);
       WCMD_show_prompt(TRUE);
@@ -1881,11 +1873,11 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE
       /* I don't know why Windows puts a space here but it does */
       /* Except for lines starting with 'echo.', 'echo:' or 'echo/'. Ask MS why */
       if (CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
-                         curPos, min_len, echoDot, len) != CSTR_EQUAL
+                         curPos, min_len, L"echo.", len) != CSTR_EQUAL
           && CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
-                         curPos, min_len, echoCol, len) != CSTR_EQUAL
+                         curPos, min_len, L"echo:", len) != CSTR_EQUAL
           && CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
-                         curPos, min_len, echoSlash, len) != CSTR_EQUAL)
+                         curPos, min_len, L"echo/", len) != CSTR_EQUAL)
       {
           WCMD_output_asis(L" ");
       }
@@ -1921,13 +1913,11 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE
 
       /* Certain commands need special handling */
       if (curStringLen == 0 && curCopyTo == curString) {
-        static const WCHAR forDO[] = {'d','o'};
-
         /* If command starts with 'rem ' or identifies a label, ignore any &&, ( etc. */
-        if (WCMD_keyword_ws_found(remCmd, ARRAY_SIZE(remCmd), curPos) || *curPos == ':') {
+        if (WCMD_keyword_ws_found(L"rem", ARRAY_SIZE(L"rem") - 1, curPos) || *curPos == ':') {
           inOneLine = TRUE;
 
-        } else if (WCMD_keyword_ws_found(forCmd, ARRAY_SIZE(forCmd), curPos)) {
+        } else if (WCMD_keyword_ws_found(L"for", ARRAY_SIZE(L"for") - 1, curPos)) {
           inFor = TRUE;
 
         /* If command starts with 'if ' or 'else ', handle ('s mid line. We should ensure this
@@ -1935,14 +1925,14 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE
            should suffice for now.
            To be able to handle ('s in the condition part take as much as evaluate_if_condition
            would take and skip parsing it here. */
-        } else if (WCMD_keyword_ws_found(ifCmd, ARRAY_SIZE(ifCmd), curPos)) {
+        } else if (WCMD_keyword_ws_found(L"if", ARRAY_SIZE(L"if") - 1, curPos)) {
           int negate; /* Negate condition */
           int test;   /* Condition evaluation result */
           WCHAR *p, *command;
 
           inIf = TRUE;
 
-          p = curPos+(ARRAY_SIZE(ifCmd));
+          p = curPos+(ARRAY_SIZE(L"if") - 1);
           while (*p == ' ' || *p == '\t')
             p++;
           WCMD_parse (p, quals, param1, param2);
@@ -1960,11 +1950,11 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE
               curPos+=if_condition_len;
           }
 
-          if (WCMD_keyword_ws_found(setCmd, ARRAY_SIZE(setCmd), curPos))
+          if (WCMD_keyword_ws_found(L"set", ARRAY_SIZE(L"set") - 1, curPos))
               ignoreBracket = TRUE;
 
-        } else if (WCMD_keyword_ws_found(ifElse, ARRAY_SIZE(ifElse), curPos)) {
-          const int keyw_len = ARRAY_SIZE(ifElse) + 1;
+        } else if (WCMD_keyword_ws_found(L"else", ARRAY_SIZE(L"else") - 1, curPos)) {
+          const int keyw_len = ARRAY_SIZE(L"else");
           inElse = TRUE;
           lastWasElse = TRUE;
           onlyWhiteSpace = TRUE;
@@ -1985,8 +1975,8 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE
         /* In a for loop, the DO command will follow a close bracket followed by
            whitespace, followed by DO, ie closeBracket inserts a NULL entry, curLen
            is then 0, and all whitespace is skipped                                */
-        } else if (inFor && WCMD_keyword_ws_found(forDO, ARRAY_SIZE(forDO), curPos)) {
-          const int keyw_len = ARRAY_SIZE(forDO) + 1;
+        } else if (inFor && WCMD_keyword_ws_found(L"do", ARRAY_SIZE(L"do") - 1, curPos)) {
+          const int keyw_len = ARRAY_SIZE(L"do");
           WINE_TRACE("Found 'DO '\n");
           lastWasDo = TRUE;
           onlyWhiteSpace = TRUE;
@@ -1999,12 +1989,10 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE
 
         /* Special handling for the 'FOR' command */
         if (inFor && lastWasWhiteSpace) {
-          static const WCHAR forIN[] = {'i','n'};
-
           WINE_TRACE("Found 'FOR ', comparing next parm: '%s'\n", wine_dbgstr_w(curPos));
 
-          if (WCMD_keyword_ws_found(forIN, ARRAY_SIZE(forIN), curPos)) {
-            const int keyw_len = ARRAY_SIZE(forIN) + 1;
+          if (WCMD_keyword_ws_found(L"in", ARRAY_SIZE(L"in") - 1, curPos)) {
+            const int keyw_len = ARRAY_SIZE(L"in");
             WINE_TRACE("Found 'IN '\n");
             lastWasIn = TRUE;
             onlyWhiteSpace = TRUE;
-- 
2.31.1




More information about the wine-devel mailing list