patch 8.0.1317: accessing freed memory in term_wait()

Problem:    Accessing freed memory in term_wait(). (Dominique Pelle)
Solution:   Check that the buffer still exists.
diff --git a/src/terminal.c b/src/terminal.c
index 6cbe343..f1b4313 100644
--- a/src/terminal.c
+++ b/src/terminal.c
@@ -3227,6 +3227,10 @@
 	{
 	    mch_check_messages();
 	    parse_queued_messages();
+	    if (!buf_valid(buf))
+		/* If the terminal is closed when the channel is closed the
+		 * buffer disappears. */
+		break;
 	    ui_delay(10L, FALSE);
 	}
 	mch_check_messages();