patch 9.1.0313: Crash when using heredoc with comment in command block

Commit: 
https://github.com/vim/vim/commit/1f5175d9af3d3f37e19f23e0e6f84caec47390f2
Author: zeertzjq <[email protected]>
Date:   Sat Apr 13 17:52:26 2024 +0200

    patch 9.1.0313: Crash when using heredoc with comment in command block
    
    Problem:  Crash when using heredoc with comment in command block.
    Solution: Handle a newline more like the end of the line, fix coverity
              warning (zeertzjq).
    
    closes: #14535
    
    Signed-off-by: zeertzjq <[email protected]>
    Signed-off-by: Yegappan Lakshmanan <[email protected]>
    Signed-off-by: Christian Brabandt <[email protected]>

diff --git a/src/charset.c b/src/charset.c
index 4dcde149f..470698f0e 100644
--- a/src/charset.c
+++ b/src/charset.c
@@ -2088,17 +2088,6 @@ skiptowhite(char_u *p)
     return p;
 }
 
-/*
- * skiptowhite: skip over text until ' ' or '  ' or newline or NUL.
- */
-    char_u *
-skiptowhite_or_nl(char_u *p)
-{
-    while (*p != ' ' && *p != '        ' && *p != NL && *p != NUL)
-       ++p;
-    return p;
-}
-
 /*
  * skiptowhite_esc: Like skiptowhite(), but also skip escaped chars
  */
diff --git a/src/evalvars.c b/src/evalvars.c
index 62728ed8a..d4dd0add2 100644
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -781,8 +781,15 @@ heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int 
vim9compile)
     int                count = 0;
     int                heredoc_in_string = FALSE;
     char_u     *line_arg = NULL;
+    char_u     *nl_ptr = vim_strchr(cmd, '
');
 
-    if (eap->ea_getline == NULL && vim_strchr(cmd, '
') == NULL)
+    if (nl_ptr != NULL)
+    {
+       heredoc_in_string = TRUE;
+       line_arg = nl_ptr + 1;
+       *nl_ptr = NUL;
+    }
+    else if (eap->ea_getline == NULL)
     {
        emsg(_(e_cannot_use_heredoc_here));
        return NULL;
@@ -826,14 +833,8 @@ heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int 
vim9compile)
     if (*cmd != NUL && *cmd != comment_char)
     {
        marker = skipwhite(cmd);
-       p = skiptowhite_or_nl(marker);
-       if (*p == NL)
-       {
-           // heredoc in a string
-           line_arg = p + 1;
-           heredoc_in_string = TRUE;
-       }
-       else if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
+       p = skiptowhite(marker);
+       if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
        {
            semsg(_(e_trailing_characters_str), p);
            return NULL;
diff --git a/src/proto/charset.pro b/src/proto/charset.pro
index a4f6c453d..a74731931 100644
--- a/src/proto/charset.pro
+++ b/src/proto/charset.pro
@@ -61,7 +61,6 @@ int vim_isalpha(int c);
 int vim_toupper(int c);
 int vim_tolower(int c);
 char_u *skiptowhite(char_u *p);
-char_u *skiptowhite_or_nl(char_u *p);
 char_u *skiptowhite_esc(char_u *p);
 long getdigits(char_u **pp);
 long getdigits_quoted(char_u **pp);
diff --git a/src/testdir/test_let.vim b/src/testdir/test_let.vim
index e6d9cae8e..974e8f1a3 100644
--- a/src/testdir/test_let.vim
+++ b/src/testdir/test_let.vim
@@ -536,6 +536,13 @@ END
   XX
   call assert_equal(['Line1'], var1)
 
+  let var1 =<< trim XX " comment
+    Line1
+      Line2
+    Line3
+  XX
+  call assert_equal(['Line1', '  Line2', 'Line3'], var1)
+
   " ignore "endfunc"
   let var1 =<< END
 something
@@ -716,15 +723,27 @@ END
   call v9.CheckScriptFailure(lines, 'E15:')
 
   " Test for using heredoc in a single string using execute()
-  call assert_equal(["['one', 'two']"],
-    \ execute("let x =<< trim END
  one
  two
END
echo x")->split("
"))
-  call assert_equal(["['  one', '  two']"],
-    \ execute("let x =<< END
  one
  two
END
echo x")->split("
"))
+  call assert_equal("
['one', 'two']",
+    \ execute("let x =<< trim END
  one
  two
END
echo x"))
+  call assert_equal("
['one', '  two']",
+    \ execute("let x =<< trim END
  one
    two
END
echo x"))
+  call assert_equal("
['one', 'two']",
+    \ execute("  let x =<< trim END
    one
    two
  END
echo x"))
+  call assert_equal("
['one', '  two']",
+    \ execute("  let x =<< trim END
    one
      two
  END
echo x"))
+  call assert_equal("
['  one', '  two']",
+    \ execute("let x =<< END
  one
  two
END
echo x"))
+  call assert_equal("
['one', 'two']",
+    \ execute("let x =<< END
one
two
END
echo x"))
+  call assert_equal("
['one', 'two']",
+    \ execute("let x =<< END \" comment
one
two
END
echo x"))
   let cmd = 'execute("let x =<< END
  one
  two
echo x")'
   call assert_fails(cmd, "E990: Missing end marker 'END'")
   let cmd = 'execute("let x =<<
  one
  two
echo x")'
-  call assert_fails(cmd, "E990: Missing end marker ''")
+  call assert_fails(cmd, "E172: Missing marker")
   let cmd = 'execute("let x =<< trim
  one
  two
echo x")'
+  call assert_fails(cmd, "E172: Missing marker")
+  let cmd = 'execute("let x =<< end
  one
  two
end
echo x")'
   call assert_fails(cmd, "E221: Marker cannot start with lower case letter")
   let cmd = 'execute("let x =<< eval END
  one
  two{y}
END
echo x")'
   call assert_fails(cmd, 'E121: Undefined variable: y')
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index 136c81db7..a3f21bed0 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -508,6 +508,22 @@ def Test_command_block_heredoc()
   CODE
   v9.CheckSourceSuccess(lines)
 
+  # Heredoc with comment
+  lines =<< trim CODE
+    vim9script
+    com SomeCommand {
+        g:someVar =<< trim END # comment
+          aaa
+          bbb
+        END
+      }
+    execute('SomeCommand')
+    assert_equal(['aaa', 'bbb'], g:someVar)
+    delcommand SomeCommand
+    unlet g:someVar
+  CODE
+  v9.CheckSourceSuccess(lines)
+
   # heredoc evaluation
   lines =<< trim CODE
     vim9script
diff --git a/src/version.c b/src/version.c
index 1a924e723..dd7f2035e 100644
--- a/src/version.c
+++ b/src/version.c
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    313,
 /**/
     312,
 /**/

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/E1rvfnS-008ICx-Fd%40256bit.org.

Raspunde prin e-mail lui