[X2Go-Commits] [libx2goclient] 01/06: src/x2goclient-network-ssh.c: use GError when parsing port specification.

git-admin at x2go.org git-admin at x2go.org
Mon Jul 29 12:13:24 CEST 2019


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch master
in repository libx2goclient.

commit cbedb4cf5851ad42acca4594d1a0bfe920f03856
Author: Mihai Moldovan <ionic at ionic.de>
Date:   Mon Jul 29 09:35:35 2019 +0200

    src/x2goclient-network-ssh.c: use GError when parsing port specification.
---
 src/x2goclient-network-ssh.c | 40 +++++++++++++++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 7 deletions(-)

diff --git a/src/x2goclient-network-ssh.c b/src/x2goclient-network-ssh.c
index 702f279..05c0775 100644
--- a/src/x2goclient-network-ssh.c
+++ b/src/x2goclient-network-ssh.c
@@ -46,6 +46,15 @@
 #include "x2goclient.h"
 #include "x2goclient-network-ssh.h"
 
+/*
+ * Error handling helpers.
+ */
+#define X2GOCLIENT_NETWORK_SSH_ERROR g_quark_from_static_string ("x2goclient-network-ssh")
+
+enum {
+  X2GOCLIENT_NETWORK_SSH_ERROR_PORT_CONV = 0
+};
+
 struct _X2GoClientNetworkOptionsSSH {
   X2GoClientNetworkOptions parent_class;
 };
@@ -108,29 +117,37 @@ static GSocketAddress* x2goclient_network_ssh_parse_sockspec_unix_socket (const
   return (ret);
 }
 
-static gint32 x2goclient_network_ssh_parse_sockspec_port (const GString *portspec) {
-  gint32 ret = -1;
+static guint16 x2goclient_network_ssh_parse_sockspec_port (const GString *portspec, GError **port_parse_err) {
+  guint16 ret = 0;
 
   if (':' == portspec->str[0]) {
     /*
      * In the worst case, portspec->str[1] will point to a terminating NULL
      * character, but that's non-critical.
      */
+    gchar *err_msg = NULL;
     errno = 0;
+    int saved_errno = 0;
     long long conv = strtoll (portspec->str + 1, NULL, 10);
+    saved_errno = errno;
 
-    if (0 != errno) {
-      g_log (NULL, G_LOG_LEVEL_WARNING, "Unable to convert port specifier, invalid input.");
+    if (0 != saved_errno) {
+      err_msg = "Not a valid number.";
     }
     else if ((1 << 16) <= conv)  {
-      g_log (NULL, G_LOG_LEVEL_WARNING, "Port specifier out of range (too big), invalid input.");
+      err_msg = "Port specifier out of range: too big.";
     }
     else if (0 >= conv) {
-      g_log (NULL, G_LOG_LEVEL_WARNING, "Port specifier out of range (negative or zero), invalid input.");
+      err_msg = "Port specifier out of range: negative or zero.";
     }
     else {
       ret = conv;
     }
+
+    if (err_msg) {
+      g_set_error_literal (port_parse_err, X2GOCLIENT_NETWORK_SSH_ERROR, X2GOCLIENT_NETWORK_SSH_ERROR_PORT_CONV, err_msg);
+      err_msg = NULL;
+    }
   }
   else {
     /* No specifier found, assume default port. */
@@ -155,6 +172,7 @@ static GSocketAddress* x2goclient_network_ssh_parse_sockspec (X2GoClientNetworkS
 
       gboolean could_be_v6 = FALSE;
       gboolean is_v6 = FALSE;
+      gboolean port_parse_err = TRUE;
       gchar *tmp_start = sockspec->str;
       gchar *tmp_end = NULL;
       GString *v6_address = NULL;
@@ -200,9 +218,17 @@ static GSocketAddress* x2goclient_network_ssh_parse_sockspec (X2GoClientNetworkS
            * fine.
            */
           GString *portspec = g_string_new (tmp_end + 2);
-          gint32 port_parse = x2goclient_network_ssh_parse_sockspec_port (portspec);
+          GError *tmp_err = NULL;
+          port = x2goclient_network_ssh_parse_sockspec_port (portspec, &tmp_err);
           g_string_free (portspec, TRUE);
           portspec = NULL;
+
+          if (tmp_err) {
+            g_log (NULL, G_LOG_LEVEL_WARNING, "Unable to convert port specifier: %s", tmp_err->message);
+          }
+          else {
+            port_parse_err = FALSE;
+          }
         }
         else {
           address = g_string_new (v6_address->str);

--
Alioth's /home/x2go-admin/maintenancescripts/git/hooks/post-receive-email on /srv/git/code.x2go.org/libx2goclient.git


More information about the x2go-commits mailing list