patch 8.2.0371: crash with combination of terminal popup and autocmd

Problem:    Crash with combination of terminal popup and autocmd.
Solution:   Disallow closing a popup that is the current window.  Add a check
            that the current buffer is valid. (closes #5754)
diff --git a/src/terminal.c b/src/terminal.c
index d4c605e..34316d8 100644
--- a/src/terminal.c
+++ b/src/terminal.c
@@ -382,6 +382,7 @@
 	curwin->w_buffer = curbuf;
 	++curbuf->b_nwindows;
     }
+    CHECK_CURBUF;
 
     // Wiping out the buffer will also close the window and call
     // free_terminal().