[X2Go-Commits] [libx2goclient] 41/132: src/x2goclient-openssh-version.{c, h}: correctly check for the validity of version components.

git-admin at x2go.org git-admin at x2go.org
Fri Dec 3 15:26:30 CET 2021


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

x2go pushed a commit to branch master
in repository libx2goclient.

commit cfe067f618c39de132e5ed560d98f29ebfb639cb
Author: Mihai Moldovan <ionic at ionic.de>
Date:   Sat Apr 24 09:12:05 2021 +0200

    src/x2goclient-openssh-version.{c,h}: correctly check for the validity of version components.
    
    The previous code took a shortcut and checked if the component was
    positive or zero, but since the backing type is unsigned, this was a
    tautology.
    
    Instead, rework this portion to use bit flags indicating whether each
    component is valid or not, initialize them correctly (to the invalid
    state) and only set them to valid if a valid value has been provided.
---
 src/x2goclient-openssh-version.c | 69 ++++++++++++++++++++++++++--------------
 src/x2goclient-openssh-version.h |  3 ++
 2 files changed, 49 insertions(+), 23 deletions(-)

diff --git a/src/x2goclient-openssh-version.c b/src/x2goclient-openssh-version.c
index 109184a..b2dcf0f 100644
--- a/src/x2goclient-openssh-version.c
+++ b/src/x2goclient-openssh-version.c
@@ -84,37 +84,59 @@ G_DEFINE_BOXED_TYPE (X2GoClientOpenSSHVersion, x2goclient_openssh_version, &x2go
       out_var = GENERATE_ERROR (PATCH, error);\
     }\
   } while (0)
-#define SET_VER_COMPONENT(out_struct, idx_var, value) \
+
+#define SET_VER_COMPONENT(ver_struct, idx_var, value, init) \
   do {\
-    if (0 == idx_var) {\
-      out_struct->major = value;\
+    _Bool valid = TRUE;\
+    if ((init)) {\
+      valid = FALSE;\
     }\
-    else if (1 == idx_var) {\
-      out_struct->minor = value;\
+    if (0 == (idx_var)) {\
+      if ((init)) {\
+        (ver_struct)->major = 0;\
+      }\
+      else {\
+        (ver_struct)->major = (value);\
+      }\
+      (ver_struct)->major_valid = valid;\
+    }\
+    else if (1 == (idx_var)) {\
+      if ((init)) {\
+        (ver_struct)->minor = 0;\
+      }\
+      else {\
+        (ver_struct)->minor = (value);\
+      }\
+      (ver_struct)->minor_valid = valid;\
     }\
     else {\
-      out_struct->patch = value;\
+      if ((init)) {\
+        (ver_struct)->patch = 0;\
+      }\
+      else {\
+        (ver_struct)->patch = (value);\
+      }\
+      (ver_struct)->patch_valid = valid;\
     }\
   } while (0)
-#define CHECK_VER_COMPONENT(out_struct, idx_var, err) \
+
+#define INIT_VER_COMPONENT(ver_struct, idx_var) SET_VER_COMPONENT ((ver_struct), (idx_var), (guint32)(0), TRUE)
+
+#define CHECK_VER_COMPONENT(ver_struct, idx_var, err) \
   do {\
-    if (0 == idx_var) {\
-      if (0 > out_struct->major) {\
-        err = TRUE;\
-        break;\
-      }\
+    _Bool new_err = (err);\
+    if (0 == (idx_var)) {\
+      new_err = (!((ver_struct)->major_valid));\
     }\
-    else if (1 == idx_var) {\
-      if (0 > out_struct->minor) {\
-        err = TRUE;\
-        break;\
-      }\
+    else if (1 == (idx_var)) {\
+      new_err = (!((ver_struct)->minor_valid));\
     }\
     else {\
-      if (0 > out_struct->patch) {\
-        err = TRUE;\
-        break;\
-      }\
+      new_err = (!((ver_struct)->patch_valid));\
+    }\
+    if (new_err) {\
+      (err) = TRUE;\
+      break;\
     }\
   } while (0)
 
@@ -264,7 +286,7 @@ _Bool x2goclient_openssh_version_parse (X2GoClientOpenSSHVersion * const openssh
 
     /* Copy and convert version number. */
     gchar *tmp_copy = g_strndup (tmp, (end_search - tmp));
-    SET_VER_COMPONENT (struct_work_copy, num_i, -1);
+    INIT_VER_COMPONENT (struct_work_copy, num_i);
 
     _Bool conv_err = TRUE,
           min_err = TRUE,
@@ -306,7 +328,8 @@ _Bool x2goclient_openssh_version_parse (X2GoClientOpenSSHVersion * const openssh
         g_set_error_literal (gerr, ERROR_QUARK, err_code, err_msg);
       }
       else {
-        SET_VER_COMPONENT (struct_work_copy, num_i, conv);
+        /* The cast here is safe since we already checked bounds. */
+        SET_VER_COMPONENT (struct_work_copy, num_i, (guint32)(conv), FALSE);
       }
     }
 
diff --git a/src/x2goclient-openssh-version.h b/src/x2goclient-openssh-version.h
index 9e21cf2..1b07d40 100644
--- a/src/x2goclient-openssh-version.h
+++ b/src/x2goclient-openssh-version.h
@@ -32,6 +32,9 @@
 G_BEGIN_DECLS
 
 typedef struct X2GoClientOpenSSHVersion_ {
+  int minor_valid:1;
+  int major_valid:1;
+  int patch_valid:1;
   guint32 major;
   guint32 minor;
   guint32 patch;

--
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