From 111acd2733bce4ec112aa0c67d01c94bb493409f Mon Sep 17 00:00:00 2001
From: Giovanni panozzo <giovanni@panozzo.it>
Date: Thu, 9 Jul 2020 18:21:42 +0200
Subject: [PATCH] Avoid quickconnect to empty hostnames. Fixes #2240.

---
 plugins/rdp/rdp_plugin.c | 19 +++++++++++++------
 src/remmina_main.c       | 20 +++++++++++++++++---
 2 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/plugins/rdp/rdp_plugin.c b/plugins/rdp/rdp_plugin.c
index 2ed2efbda8..349243ddab 100644
--- a/plugins/rdp/rdp_plugin.c
+++ b/plugins/rdp/rdp_plugin.c
@@ -295,6 +295,9 @@ static gboolean remmina_rdp_tunnel_init(RemminaProtocolWidget *gp)
 
 	remmina_plugin_service->get_server_port(hostport, 3389, &host, &port);
 
+	if (host[0] == 0)
+		return FALSE;
+
 	REMMINA_PLUGIN_DEBUG("protocol_plugin_start_direct_tunnel() returned %s", hostport);
 
 	cert_host = host;
@@ -1942,7 +1945,7 @@ static gboolean remmina_rdp_open_connection(RemminaProtocolWidget *gp)
 
 	if (pthread_create(&rfi->remmina_plugin_thread, NULL, remmina_rdp_main_thread, gp)) {
 		remmina_plugin_service->protocol_plugin_set_error(gp, "%s",
-								  "Could not start pthread. Falling back to non-thread mode…");
+								  "Could not start pthread.");
 
 		rfi->remmina_plugin_thread = 0;
 
@@ -1953,11 +1956,15 @@ static gboolean remmina_rdp_open_connection(RemminaProtocolWidget *gp)
 	profile_name = remmina_plugin_service->file_get_string(remminafile, "name");
 	p = profile_name;
 	strcpy(thname, "RemmRDP:");
-	nthname = strlen(thname);
-	while ((c = *p) != 0 && nthname < sizeof(thname) - 1) {
-		if (isalnum(c))
-			thname[nthname++] = c;
-		p++;
+	if (p != NULL) {
+		nthname = strlen(thname);
+		while ((c = *p) != 0 && nthname < sizeof(thname) - 1) {
+			if (isalnum(c))
+				thname[nthname++] = c;
+			p++;
+		}
+	} else {
+		strcat(thname, "<NONAM>");
 	}
 	thname[nthname] = 0;
 #if defined(__linux__)
diff --git a/src/remmina_main.c b/src/remmina_main.c
index 8c847c0e6e..600c8d4810 100644
--- a/src/remmina_main.c
+++ b/src/remmina_main.c
@@ -35,6 +35,7 @@
  */
 
 #include "config.h"
+#include <ctype.h>
 #include <gio/gio.h>
 #include <gio/gdesktopappinfo.h>
 #include <gdk/gdkkeysyms.h>
@@ -1043,11 +1044,23 @@ void remmina_main_on_action_application_news(GSimpleAction *action, GVariant *pa
 	remmina_pref_save();
 };
 
+static gboolean is_empty(const gchar *s)
+{
+	if (s == NULL)
+		return TRUE;
+	while (*s != 0) {
+		if (!isspace((unsigned char)*s))
+			return FALSE;
+		s++;
+	}
+	return TRUE;
+}
+
 static gboolean remmina_main_quickconnect(void)
 {
 	TRACE_CALL(__func__);
 	RemminaFile *remminafile;
-	gchar *server;
+	const gchar *server;
 	gchar *qcp;
 
 
@@ -1060,13 +1073,14 @@ static gboolean remmina_main_quickconnect(void)
 	}
 
 	remminafile = remmina_file_new();
-	server = g_strdup(gtk_entry_get_text(remminamain->entry_quick_connect_server));
+	server = gtk_entry_get_text(remminamain->entry_quick_connect_server);
+	if (is_empty(server))
+		return FALSE;
 
 	remmina_file_set_string(remminafile, "sound", "off");
 	remmina_file_set_string(remminafile, "server", server);
 	remmina_file_set_string(remminafile, "name", server);
 	remmina_file_set_string(remminafile, "protocol", qcp);
-	g_free(server);
 
 	rcw_open_from_file(remminafile);
 
-- 
GitLab