patch 9.1.0062: Internal error when :luado/perldo/pydo etc delete lines

Problem:  Internal error when :luado/perldo/pydo etc delete lines
Solution: Test that the line is still valid line number
          (zeertzjq)

closes: #13931

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/if_lua.c b/src/if_lua.c
index 159a675..4d19c3e 100644
--- a/src/if_lua.c
+++ b/src/if_lua.c
@@ -2692,9 +2692,12 @@
 	    luaV_emsg(L);
 	    break;
 	}
+
 	// Catch the command switching to another buffer.
-	if (curbuf != was_curbuf)
+	// Check the line number, the command may have deleted lines.
+	if (curbuf != was_curbuf || l > curbuf->b_ml.ml_line_count)
 	    break;
+
 	if (lua_isstring(L, -1)) // update line?
 	{
 #ifdef HAVE_SANDBOX
diff --git a/src/if_perl.xs b/src/if_perl.xs
index dd504d3..4f2f8ac 100644
--- a/src/if_perl.xs
+++ b/src/if_perl.xs
@@ -1368,7 +1368,7 @@
 	PUSHMARK(sp);
 	perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
 	str = SvPV(GvSV(PL_errgv), length);
-	if (length || curbuf != was_curbuf)
+	if (length || curbuf != was_curbuf || i > curbuf->b_ml.ml_line_count)
 	    break;
 	SPAGAIN;
 	if (SvTRUEx(POPs))
diff --git a/src/if_py_both.h b/src/if_py_both.h
index 42db510..3e5993b 100644
--- a/src/if_py_both.h
+++ b/src/if_py_both.h
@@ -6136,7 +6136,8 @@
 	    goto err;
 
 	// Check that the command didn't switch to another buffer.
-	if (curbuf != was_curbuf)
+	// Check the line number, the command my have deleted lines.
+	if (curbuf != was_curbuf || lnum > curbuf->b_ml.ml_line_count)
 	{
 	    Py_XDECREF(ret);
 	    goto err;
diff --git a/src/if_ruby.c b/src/if_ruby.c
index 7fab211..a018dcd 100644
--- a/src/if_ruby.c
+++ b/src/if_ruby.c
@@ -885,7 +885,7 @@
 	    error_print(state);
 	    break;
 	}
-	if (was_curbuf != curbuf)
+	if (was_curbuf != curbuf || i > curbuf->b_ml.ml_line_count)
 	    break;
 	line = rb_lastline_get();
 	if (!NIL_P(line))
diff --git a/src/if_tcl.c b/src/if_tcl.c
index d3d4639..eab321b 100644
--- a/src/if_tcl.c
+++ b/src/if_tcl.c
@@ -2012,7 +2012,8 @@
 #if (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION >= 5) || TCL_MAJOR_VERSION > 8
 	    || Tcl_LimitExceeded(tclinfo.interp)
 #endif
-	    || curbuf != was_curbuf)
+	    || curbuf != was_curbuf
+	    || (linenr_T)rs > curbuf->b_ml.ml_line_count)
 	    break;
 	line = (char *)Tcl_GetVar(tclinfo.interp, var_line, 0);
 	if (line)
diff --git a/src/testdir/test_lua.vim b/src/testdir/test_lua.vim
index 7e72728..a99affc 100644
--- a/src/testdir/test_lua.vim
+++ b/src/testdir/test_lua.vim
@@ -28,21 +28,37 @@
 endfunc
 
 " Check that switching to another buffer does not trigger ml_get error.
-func Test_lua_command_new_no_ml_get_error()
+func Test_lua_luado_change_buffer()
   new
+
   let wincount = winnr('$')
   call setline(1, ['one', 'two', 'three'])
   luado vim.command("new")
   call assert_equal(wincount + 1, winnr('$'))
+
   %bwipe!
 endfunc
 
-" Test vim.command()
-func Test_lua_command()
+" Check that :luado deleting lines does not trigger ml_get error.
+func Test_lua_luado_delete_lines()
   new
+
+  call setline(1, ['one', 'two', 'three'])
+  luado vim.command("%d_")
+  call assert_equal([''], getline(1, '$'))
+
   call setline(1, ['one', 'two', 'three'])
   luado vim.command("1,2d_")
   call assert_equal(['three'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  luado vim.command("2,3d_"); return "REPLACED"
+  call assert_equal(['REPLACED'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  2,3luado vim.command("1,2d_"); return "REPLACED"
+  call assert_equal(['three'], getline(1, '$'))
+
   bwipe!
 endfunc
 
diff --git a/src/testdir/test_perl.vim b/src/testdir/test_perl.vim
index 681aaae..721179c 100644
--- a/src/testdir/test_perl.vim
+++ b/src/testdir/test_perl.vim
@@ -211,10 +211,25 @@
   call assert_false(search('\Cperl'))
   bw!
 
-  " Check deleting lines does not trigger ml_get error.
   new
+
+  " Check deleting lines does not trigger ml_get error.
   call setline(1, ['one', 'two', 'three'])
   perldo VIM::DoCommand("%d_")
+  call assert_equal([''], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  perldo VIM::DoCommand("1,2d_")
+  call assert_equal(['three'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  perldo VIM::DoCommand("2,3d_"); $_ = "REPLACED"
+  call assert_equal(['REPLACED'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  2,3perldo VIM::DoCommand("1,2d_"); $_ = "REPLACED"
+  call assert_equal(['three'], getline(1, '$'))
+
   bwipe!
 
   " Check a Perl expression which gives an error.
diff --git a/src/testdir/test_python2.vim b/src/testdir/test_python2.vim
index 066b4bd..ac43e60 100644
--- a/src/testdir/test_python2.vim
+++ b/src/testdir/test_python2.vim
@@ -56,10 +56,25 @@
 endfunc
 
 func Test_pydo()
-  " Check deleting lines does not trigger an ml_get error.
   new
+
+  " Check deleting lines does not trigger an ml_get error.
   call setline(1, ['one', 'two', 'three'])
   pydo vim.command("%d_")
+  call assert_equal([''], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  pydo vim.command("1,2d_")
+  call assert_equal(['three'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  pydo vim.command("2,3d_"); return "REPLACED"
+  call assert_equal(['REPLACED'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  2,3pydo vim.command("1,2d_"); return "REPLACED"
+  call assert_equal(['three'], getline(1, '$'))
+
   bwipe!
 
   " Check switching to another buffer does not trigger an ml_get error.
diff --git a/src/testdir/test_python3.vim b/src/testdir/test_python3.vim
index e975cb6..7f04f11 100644
--- a/src/testdir/test_python3.vim
+++ b/src/testdir/test_python3.vim
@@ -88,10 +88,25 @@
 endfunc
 
 func Test_py3do()
-  " Check deleting lines does not trigger an ml_get error.
   new
+
+  " Check deleting lines does not trigger an ml_get error.
   call setline(1, ['one', 'two', 'three'])
   py3do vim.command("%d_")
+  call assert_equal([''], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  py3do vim.command("1,2d_")
+  call assert_equal(['three'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  py3do vim.command("2,3d_"); return "REPLACED"
+  call assert_equal(['REPLACED'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  2,3py3do vim.command("1,2d_"); return "REPLACED"
+  call assert_equal(['three'], getline(1, '$'))
+
   bwipe!
 
   " Check switching to another buffer does not trigger an ml_get error.
diff --git a/src/testdir/test_ruby.vim b/src/testdir/test_ruby.vim
index 2e0835c..f155168 100644
--- a/src/testdir/test_ruby.vim
+++ b/src/testdir/test_ruby.vim
@@ -11,10 +11,25 @@
 endfunc
 
 func Test_rubydo()
-  " Check deleting lines does not trigger ml_get error.
   new
+
+  " Check deleting lines does not trigger ml_get error.
   call setline(1, ['one', 'two', 'three'])
   rubydo Vim.command("%d_")
+  call assert_equal(['one'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  rubydo Vim.command("1,2d_")
+  call assert_equal(['one'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  rubydo Vim.command("2,3d_"); $_ = "REPLACED"
+  call assert_equal(['REPLACED'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  2,3rubydo Vim.command("1,2d_"); $_ = "REPLACED"
+  call assert_equal(['three'], getline(1, '$'))
+
   bwipe!
 
   " Check switching to another buffer does not trigger ml_get error.
diff --git a/src/testdir/test_tcl.vim b/src/testdir/test_tcl.vim
index 4c6537f..68a6d63 100644
--- a/src/testdir/test_tcl.vim
+++ b/src/testdir/test_tcl.vim
@@ -11,10 +11,25 @@
 endfunc
 
 func Test_tcldo()
-  " Check deleting lines does not trigger ml_get error.
   new
+
+  " Check deleting lines does not trigger ml_get error.
   call setline(1, ['one', 'two', 'three'])
   tcldo ::vim::command %d_
+  call assert_equal(['one'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  tcldo ::vim::command 1,2d_
+  call assert_equal(['one'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  tcldo ::vim::command 2,3d_ ; set line REPLACED
+  call assert_equal(['REPLACED'], getline(1, '$'))
+
+  call setline(1, ['one', 'two', 'three'])
+  2,3tcldo ::vim::command 1,2d_ ; set line REPLACED
+  call assert_equal(['three'], getline(1, '$'))
+
   bwipe!
 
   " Check that switching to another buffer does not trigger ml_get error.
diff --git a/src/version.c b/src/version.c
index 749a918..e9917e1 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    62,
+/**/
     61,
 /**/
     60,