[gnome-flashback] monitor-config-store: fix incorrect string comparison with empty string



commit 827af5e395e9014cdde6bb86314c719cb33304ef
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Mon Mar 7 07:06:26 2022 +0200

    monitor-config-store: fix incorrect string comparison with empty string
    
    strncmp() always return 0 if the passed length is 0. What this means is
    that whatever the first string check happens to be, if the parsed XML
    cdata was empty (e.g. if we got <element></element>), the first
    condition would evaluate to true, which is rather unexpected.
    
    Fix this by making sure the string length is correct first. Also move it
    into a helper so we don't need to repeat the same strlen() check every
    time.
    
    Based on mutter commit:
    https://gitlab.gnome.org/GNOME/mutter/-/commit/6c4380ed417782594f790de246648b56e118420b

 backends/gf-monitor-config-store.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
---
diff --git a/backends/gf-monitor-config-store.c b/backends/gf-monitor-config-store.c
index 945242b..a509a93 100644
--- a/backends/gf-monitor-config-store.c
+++ b/backends/gf-monitor-config-store.c
@@ -203,6 +203,17 @@ is_system_config (GfMonitorsConfig *config)
   return !!(config->flags & GF_MONITORS_CONFIG_FLAG_SYSTEM_CONFIG);
 }
 
+static gboolean
+text_equals (const char *text,
+             size_t      len,
+             const char *expect)
+{
+  if (strlen (expect) != len)
+    return FALSE;
+
+  return strncmp (text, expect, len) == 0;
+}
+
 static void
 enter_unknown_element (ConfigParser *parser,
                        const char   *element_name,
@@ -949,12 +960,12 @@ read_bool (const gchar  *text,
            gboolean     *out_value,
            GError      **error)
 {
-  if (strncmp (text, "no", text_len) == 0)
+  if (text_equals (text, text_len, "no"))
     {
       *out_value = FALSE;
       return TRUE;
     }
-  else if (strncmp (text, "yes", text_len) == 0)
+  else if (text_equals (text, text_len, "yes"))
     {
       *out_value = TRUE;
       return TRUE;
@@ -1084,13 +1095,13 @@ handle_text (GMarkupParseContext  *context,
 
       case STATE_TRANSFORM_ROTATION:
         {
-          if (strncmp (text, "normal", text_len) == 0)
+          if (text_equals (text, text_len, "normal"))
             parser->current_transform = GF_MONITOR_TRANSFORM_NORMAL;
-          else if (strncmp (text, "left", text_len) == 0)
+          else if (text_equals (text, text_len, "left"))
             parser->current_transform = GF_MONITOR_TRANSFORM_90;
-          else if (strncmp (text, "upside_down", text_len) == 0)
+          else if (text_equals (text, text_len, "upside_down"))
             parser->current_transform = GF_MONITOR_TRANSFORM_180;
-          else if (strncmp (text, "right", text_len) == 0)
+          else if (text_equals (text, text_len, "right"))
             parser->current_transform = GF_MONITOR_TRANSFORM_270;
           else
             g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
@@ -1133,7 +1144,7 @@ handle_text (GMarkupParseContext  *context,
 
       case STATE_MONITOR_MODE_FLAG:
         {
-          if (strncmp (text, "interlace", text_len) == 0)
+          if (text_equals (text, text_len, "interlace"))
             {
               parser->current_monitor_mode_spec->flags |= GF_CRTC_MODE_FLAG_INTERLACE;
             }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]