patch 8.2.1748: closing split window in other tab may cause a crash

Problem:    Closing split window in other tab may cause a crash.
Solution:   Set tp_curwin properly. (Rob Pilling, closes #7018)
diff --git a/src/testdir/test_winbuf_close.vim b/src/testdir/test_winbuf_close.vim
index 1d3e357..2530be0 100644
--- a/src/testdir/test_winbuf_close.vim
+++ b/src/testdir/test_winbuf_close.vim
@@ -192,7 +192,23 @@
   call win_execute(l:wid, 'close')
   " Should not crash.
   call assert_true(v:true)
-  %bwipe!
+
+  " This tests closing a window in another tab, while leaving the tab open
+  " i.e. two windows in another tab.
+  tabedit
+  let w:this_win = 42
+  new
+  let othertab_wid = win_getid()
+  tabprevious
+  call win_execute(othertab_wid, 'q')
+  " drawing the tabline helps check that the other tab's windows and buffers
+  " are still valid
+  redrawtabline
+  " but to be certain, ensure we can focus the other tab too
+  tabnext
+  call assert_equal(42, w:this_win)
+
+  bwipe!
 endfunc
 
 " Test when closing a split window (above/below) restores space to the window
diff --git a/src/version.c b/src/version.c
index 03765bf..c253d09 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1748,
+/**/
     1747,
 /**/
     1746,
diff --git a/src/window.c b/src/window.c
index 75e0495..c73062d 100644
--- a/src/window.c
+++ b/src/window.c
@@ -2745,6 +2745,7 @@
 {
     frame_T	*frp;
     win_T	*wp;
+    tabpage_T	*win_tp = tp == NULL ? curtab : tp;
 
     // Remove the window and its frame from the tree of frames.
     frp = win->w_frame;
@@ -2752,10 +2753,10 @@
     vim_free(frp);
     win_free(win, tp);
 
-    // When deleting the current window of another tab page select a new
-    // current window.
-    if (tp != NULL && win == tp->tp_curwin)
-	tp->tp_curwin = wp;
+    // When deleting the current window in the tab, select a new current
+    // window.
+    if (win == win_tp->tp_curwin)
+	win_tp->tp_curwin = wp;
 
     return wp;
 }