diff --git a/cli-main.c b/cli-main.c
index ff2b30faf2b5fda5d1fe2af35a88e2486502426e..c7c90359dc4567c18f253522b072db6546622327 100644
--- a/cli-main.c
+++ b/cli-main.c
@@ -87,6 +87,7 @@ int main(int argc, char ** argv) {
 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) {
 
 	char fmtbuf[300];
+	char exitmsg[500];
 
 	if (!sessinitdone) {
 		snprintf(fmtbuf, sizeof(fmtbuf), "Exited: %s",
@@ -98,12 +99,15 @@ static void cli_dropbear_exit(int exitcode, const char* format, va_list param) {
 				cli_opts.remoteport, format);
 	}
 
+	/* Arguments to the exit printout may be unsafe to use after session_cleanup() */
+	vsnprintf(exitmsg, sizeof(exitmsg), fmtbuf, param);
+
 	/* Do the cleanup first, since then the terminal will be reset */
 	session_cleanup();
 	/* Avoid printing onwards from terminal cruft */
 	fprintf(stderr, "\n");
 
-	_dropbear_log(LOG_INFO, fmtbuf, param);
+	dropbear_log(LOG_INFO, "%s", exitmsg);;
 	exit(exitcode);
 }
 
diff --git a/netio.c b/netio.c
index 9c84958a5465d8397106edd90ee7ad12270a2ce7..5d1e9a461521f2dc61f8bf1282a9fa55d9cd9bed 100644
--- a/netio.c
+++ b/netio.c
@@ -104,6 +104,8 @@ static void connect_try_next(struct dropbear_progress_connection *c) {
 			message.msg_iovlen = iovlen;
 			res = sendmsg(c->sock, &message, MSG_FASTOPEN);
 			if (res < 0 && errno != EINPROGRESS) {
+				m_free(c->errstring);
+				c->errstring = m_strdup(strerror(errno));
 				/* Not entirely sure which kind of errors are normal - 2.6.32 seems to 
 				return EPIPE for any (nonblocking?) sendmsg(). just fall back */
 				TRACE(("sendmsg tcp_fastopen failed, falling back. %s", strerror(errno)));
@@ -124,6 +126,8 @@ static void connect_try_next(struct dropbear_progress_connection *c) {
 
 		if (res < 0 && errno != EINPROGRESS) {
 			/* failure */
+			m_free(c->errstring);
+			c->errstring = m_strdup(strerror(errno));
 			close(c->sock);
 			c->sock = -1;
 			continue;